ModuleUpdatePacksTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. * `db_prefix` names the module's tables, so a pack must carry the module's own.
  7. */
  8. class ModuleUpdatePacksTest extends \PHPUnit\Framework\TestCase
  9. {
  10. private static function modulesPath(): string
  11. {
  12. return __DIR__ . '/../../../modules/';
  13. }
  14. private static function readConfig(string $sPath): array
  15. {
  16. $aConfig = null;
  17. include($sPath);
  18. if (!is_array($aConfig))
  19. self::fail(substr($sPath, strlen(self::modulesPath())) . ' does not define $aConfig.');
  20. return $aConfig;
  21. }
  22. public function testPacksMatchTheirModule(): void
  23. {
  24. $sModules = self::modulesPath();
  25. $aPacks = glob($sModules . '*/*/updates/*/install/config.php');
  26. $this->assertNotEmpty($aPacks, 'The repository has module update packs.');
  27. $aModules = [];
  28. $aErrors = [];
  29. foreach ($aPacks as $sPath) {
  30. $sPack = substr(dirname($sPath, 2), strlen($sModules)) . '/';
  31. $aPack = self::readConfig($sPath);
  32. $sModuleDir = $aPack['module_dir'] ?? '';
  33. if (strpos($sPack, $sModuleDir . 'updates/') !== 0) {
  34. $aErrors[] = "$sPack: module_dir '$sModuleDir' is not the module the pack is in";
  35. continue;
  36. }
  37. if (!isset($aModules[$sModuleDir]))
  38. $aModules[$sModuleDir] = self::readConfig($sModules . $sModuleDir . 'install/config.php');
  39. $aModule = $aModules[$sModuleDir];
  40. if (($aPack['module_uri'] ?? '') !== $aModule['home_uri'])
  41. $aErrors[] = "$sPack: module_uri '" . ($aPack['module_uri'] ?? '') . "' is not the module's home_uri '{$aModule['home_uri']}'";
  42. if (($aPack['class_prefix'] ?? '') !== $aModule['class_prefix'])
  43. $aErrors[] = "$sPack: class_prefix '" . ($aPack['class_prefix'] ?? '') . "' is not the module's '{$aModule['class_prefix']}'";
  44. $sDbPrefix = $aModule['db_prefix'] ?? '';
  45. if (($aPack['db_prefix'] ?? '') !== $sDbPrefix)
  46. $aErrors[] = "$sPack: db_prefix '" . ($aPack['db_prefix'] ?? '') . "' is not the module's '$sDbPrefix'";
  47. $sVersions = ($aPack['version_from'] ?? '') . '_' . ($aPack['version_to'] ?? '');
  48. if (basename($sPack) !== $sVersions)
  49. $aErrors[] = "$sPack: the folder is not named after version_from and version_to ($sVersions)";
  50. if (($aPack['home_dir'] ?? '') !== $sModuleDir . 'updates/update_' . $sVersions . '/')
  51. $aErrors[] = "$sPack: home_dir '" . ($aPack['home_dir'] ?? '') . "' is not '{$sModuleDir}updates/update_{$sVersions}/'";
  52. }
  53. $this->assertSame([], $aErrors, "Update packs that don't match their module:\n" . implode("\n", $aErrors));
  54. }
  55. }