ModuleUpdatePacksTest.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. /**
  3. * Module update packs (modules/<vendor>/<module>/updates/<from>_<to>/) must name the module they update the way the
  4. * installed module is recorded. BxDolStudioUpdater finds the module by `module_dir` and `module_uri`, and builds the
  5. * updater class name from `class_prefix`, so a pack that differs from the module's own config fails on every site.
  6. */
  7. class ModuleUpdatePacksTest extends \PHPUnit\Framework\TestCase
  8. {
  9. private static function modulesPath(): string
  10. {
  11. return __DIR__ . '/../../../modules/';
  12. }
  13. private static function readConfig(string $sPath): array
  14. {
  15. $aConfig = null;
  16. include($sPath);
  17. if (!is_array($aConfig))
  18. self::fail(substr($sPath, strlen(self::modulesPath())) . ' does not define $aConfig.');
  19. return $aConfig;
  20. }
  21. public function testPacksMatchTheirModule(): void
  22. {
  23. $sModules = self::modulesPath();
  24. $aPacks = glob($sModules . '*/*/updates/*/install/config.php');
  25. $this->assertNotEmpty($aPacks, 'The repository has module update packs.');
  26. $aModules = [];
  27. $aErrors = [];
  28. foreach ($aPacks as $sPath) {
  29. $sPack = substr(dirname($sPath, 2), strlen($sModules)) . '/';
  30. $aPack = self::readConfig($sPath);
  31. $sModuleDir = $aPack['module_dir'] ?? '';
  32. if (strpos($sPack, $sModuleDir . 'updates/') !== 0) {
  33. $aErrors[] = "$sPack: module_dir '$sModuleDir' is not the module the pack is in";
  34. continue;
  35. }
  36. if (!isset($aModules[$sModuleDir]))
  37. $aModules[$sModuleDir] = self::readConfig($sModules . $sModuleDir . 'install/config.php');
  38. $aModule = $aModules[$sModuleDir];
  39. if (($aPack['module_uri'] ?? '') !== $aModule['home_uri'])
  40. $aErrors[] = "$sPack: module_uri '" . ($aPack['module_uri'] ?? '') . "' is not the module's home_uri '{$aModule['home_uri']}'";
  41. if (($aPack['class_prefix'] ?? '') !== $aModule['class_prefix'])
  42. $aErrors[] = "$sPack: class_prefix '" . ($aPack['class_prefix'] ?? '') . "' is not the module's '{$aModule['class_prefix']}'";
  43. $sVersions = ($aPack['version_from'] ?? '') . '_' . ($aPack['version_to'] ?? '');
  44. if (basename($sPack) !== $sVersions)
  45. $aErrors[] = "$sPack: the folder is not named after version_from and version_to ($sVersions)";
  46. if (($aPack['home_dir'] ?? '') !== $sModuleDir . 'updates/update_' . $sVersions . '/')
  47. $aErrors[] = "$sPack: home_dir '" . ($aPack['home_dir'] ?? '') . "' is not '{$sModuleDir}updates/update_{$sVersions}/'";
  48. }
  49. $this->assertSame([], $aErrors, "Update packs that would not apply:\n" . implode("\n", $aErrors));
  50. }
  51. }