diff --git a/src/Service/VersionService.php b/src/Service/VersionService.php index c6b36f5..b696709 100644 --- a/src/Service/VersionService.php +++ b/src/Service/VersionService.php @@ -15,6 +15,7 @@ use TYPO3\Tailor\Environment\Variables; use TYPO3\Tailor\Exception\FormDataProcessingException; use TYPO3\Tailor\Exception\RequiredConfigurationMissing; +use TYPO3\Tailor\Validation\EmConfValidationError; use TYPO3\Tailor\Validation\EmConfVersionValidator; use ZipArchive; @@ -63,7 +64,7 @@ public function createZipArchiveFromPath(string $path): string $zipArchive = new \ZipArchive(); $zipArchive->open($this->getVersionFilename(), \ZipArchive::CREATE | \ZipArchive::OVERWRITE); - $emConfValid = false; + $emConfValidationErrors = [EmConfValidationError::NOT_FOUND]; $iterator = new \RecursiveDirectoryIterator($fullPath, \FilesystemIterator::SKIP_DOTS); $files = new \RecursiveIteratorIterator( @@ -110,15 +111,15 @@ public function createZipArchiveFromPath(string $path): string } if ($filename === 'ext_emconf.php') { - $emConfValid = (new EmConfVersionValidator($fileRealPath))->isValid($this->version); + $emConfValidationErrors = (new EmConfVersionValidator($fileRealPath))->collectErrors($this->version); } // Add the files including their directories $zipArchive->addFile($fileRealPath, substr($fileRealPath, strlen($fullPath) + 1)); } - if (!$emConfValid) { - throw new FormDataProcessingException('No or invalid ext_emconf.php found in the folder.', 1605563410); + if ($emConfValidationErrors !== []) { + throw new FormDataProcessingException($this->formatEmConfValidationErrors($emConfValidationErrors), 1605563410); } $zipArchive->close(); @@ -233,4 +234,18 @@ protected function getExcludeConfiguration(): array return $configuration; } + + /** + * @param list $errors + */ + private function formatEmConfValidationErrors(array $errors): string + { + $messageParts = ['Validation of `ext_emconf.php` file failed due to the following errors:']; + + foreach ($errors as $error) { + $messageParts[] = ' * ' . EmConfValidationError::getErrorMessage($error); + } + + return implode(PHP_EOL, $messageParts); + } } diff --git a/src/Validation/EmConfValidationError.php b/src/Validation/EmConfValidationError.php new file mode 100644 index 0000000..3edab7b --- /dev/null +++ b/src/Validation/EmConfValidationError.php @@ -0,0 +1,49 @@ + List of validation errors. If list is empty, ext_emconf.php file is valid. */ - public function isValid(string $givenVersion): bool + public function collectErrors(string $givenVersion): array { + if (!file_exists($this->emConfFilePath)) { + return [EmConfValidationError::NOT_FOUND]; + } + $_EXTKEY = 'dummy'; @include $this->emConfFilePath; + if (!isset($EM_CONF)) { - return false; + return [EmConfValidationError::MISSING_CONFIGURATION]; } + $emConfDetails = reset($EM_CONF); + if (!is_array($emConfDetails)) { - return false; + return [EmConfValidationError::UNSUPPORTED_TYPE]; } - if (!isset($emConfDetails['version'], $emConfDetails['constraints']['depends']['typo3'])) { - return false; + + $errors = []; + + if (!isset($emConfDetails['version'])) { + $errors[] = EmConfValidationError::MISSING_EXTENSION_VERSION; + } elseif ((string)$emConfDetails['version'] !== $givenVersion) { + $errors[] = EmConfValidationError::EXTENSION_VERSION_MISMATCH; } - if ((string)$emConfDetails['version'] !== $givenVersion) { - return false; + if (!isset($emConfDetails['constraints']['depends']['typo3'])) { + $errors[] = EmConfValidationError::MISSING_TYPO3_VERSION_CONSTRAINT; } - return true; + + return $errors; + } + + /** + * @param string $givenVersion + * @return bool TRUE if the ext_emconf is valid, FALSE otherwise + */ + public function isValid(string $givenVersion): bool + { + return $this->collectErrors($givenVersion) === []; } } diff --git a/tests/Unit/Fixtures/EmConf/emconf_no_version.php b/tests/Unit/Fixtures/EmConf/emconf_no_version.php index ddd457c..6d551c8 100644 --- a/tests/Unit/Fixtures/EmConf/emconf_no_version.php +++ b/tests/Unit/Fixtures/EmConf/emconf_no_version.php @@ -1,5 +1,5 @@ 'YES', ]; diff --git a/tests/Unit/Validation/EmConfVersionValidatorTest.php b/tests/Unit/Validation/EmConfVersionValidatorTest.php index f8c7212..a5559b2 100644 --- a/tests/Unit/Validation/EmConfVersionValidatorTest.php +++ b/tests/Unit/Validation/EmConfVersionValidatorTest.php @@ -13,10 +13,73 @@ namespace TYPO3\Tailor\Tests\Unit\Validation; use PHPUnit\Framework\TestCase; +use TYPO3\Tailor\Validation\EmConfValidationError; use TYPO3\Tailor\Validation\EmConfVersionValidator; class EmConfVersionValidatorTest extends TestCase { + /** + * @test + */ + public function collectErrorsReturnsErrorIfFileDoesNotExist(): void + { + $subject = new EmConfVersionValidator(__DIR__ . '/no-file'); + $expected = [EmConfValidationError::NOT_FOUND]; + self::assertSame($expected, $subject->collectErrors('1.2.0')); + } + + /** + * @test + */ + public function collectErrorsReturnsErrorIfConfigurationIsMissing(): void + { + $subject = new EmConfVersionValidator(__DIR__ . '/../Fixtures/EmConf/emconf_invalid.php'); + $expected = [EmConfValidationError::MISSING_CONFIGURATION]; + self::assertSame($expected, $subject->collectErrors('1.0.0')); + } + + /** + * @test + */ + public function collectErrorsReturnsErrorIfFileDoesNotMatchEmConfStructure(): void + { + $subject = new EmConfVersionValidator(__DIR__ . '/../Fixtures/EmConf/emconf_no_structure.php'); + $expected = [EmConfValidationError::UNSUPPORTED_TYPE]; + self::assertSame($expected, $subject->collectErrors('1.0.0')); + } + + /** + * @test + */ + public function collectErrorsReturnsErrorsIfNoVersionGiven(): void + { + $subject = new EmConfVersionValidator(__DIR__ . '/../Fixtures/EmConf/emconf_no_version.php'); + $expected = [ + EmConfValidationError::MISSING_EXTENSION_VERSION, + EmConfValidationError::MISSING_TYPO3_VERSION_CONSTRAINT, + ]; + self::assertSame($expected, $subject->collectErrors('1.0.0')); + } + + /** + * @test + */ + public function collectErrorsReturnsErrorIfVersionsDoNotMatch(): void + { + $subject = new EmConfVersionValidator(__DIR__ . '/../Fixtures/EmConf/emconf_valid.php'); + $expected = [EmConfValidationError::EXTENSION_VERSION_MISMATCH]; + self::assertSame($expected, $subject->collectErrors('2.0.0')); + } + + /** + * @test + */ + public function collectErrorsReturnsEmptyArrayIfFileIsValid(): void + { + $subject = new EmConfVersionValidator(__DIR__ . '/../Fixtures/EmConf/emconf_valid.php'); + self::assertSame([], $subject->collectErrors('1.0.0')); + } + /** * @test */