diff --git a/tests/HandlerTest.php b/tests/HandlerTest.php index df27cdaf8e20e55b3c2f5d5cc0e2a01d21116b19..6043c06bf35245301d45bb791bf804e8cca22b51 100644 --- a/tests/HandlerTest.php +++ b/tests/HandlerTest.php @@ -3,33 +3,80 @@ use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; use SpipContrib\Odt2Spip\Handler; +use Symfony\Component\Filesystem\Filesystem; class HandlerTest extends TestCase { - #[DataProvider('identifyImagesDataProvider')] - public function testIidentifyImages(array $expected, array $args): void { + private string $odt = _DIR_PLUGIN_ODT2SPIP . 'tests/data/handler/unzip.odt'; + + private string $docx = _DIR_PLUGIN_ODT2SPIP . 'tests/data/handler/conversion.docx'; + + public function testPrepareFile(): void { + // Créer un faux fichier et l'ajouter au tableau $_FILES + $fs = new Filesystem(); + $fs->copy(_DIR_PLUGIN_ODT2SPIP . 'tests/data/unzip.odt', $this->odt); + + // Cf. https://discourse.cakephp.org/t/unit-testing-of-file-uploads-solved/5241 + $_FILES = [ + 'odt' => [ + 'error' => UPLOAD_ERR_OK, + 'name' => 'unzip.odt', + 'size' => filesize($this->odt), + 'tmp_name' => $this->odt, + 'type' => 'application/vnd.oasis.opendocument.text', + ] + ]; + + $handler = new Handler('odt', 'article', 2); + + $this->assertNull($handler->prepareFile()); + } + + public function testPrepareFileWithConversion(): void { + // Créer un faux fichier et l'ajouter au tableau $_FILES + $fs = new Filesystem(); + $fs->copy(_DIR_PLUGIN_ODT2SPIP . 'tests/data/conversion.docx', $this->docx); + + // Cf. https://discourse.cakephp.org/t/unit-testing-of-file-uploads-solved/5241 + $_FILES = [ + 'docx' => [ + 'error' => UPLOAD_ERR_OK, + 'name' => 'conversion.docx', + 'size' => filesize($this->docx), + 'tmp_name' => $this->docx, + 'type' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', + ] + ]; + + $handler = new Handler('docx', 'article', 2); + + $this->assertNull($handler->prepareFile()); + } + + #[DataProvider('getImagesDataProvider')] + public function testGetImages(array $expected, array $args): void { $handler = new Handler('fichier', 'article', 2); $reflection = new ReflectionClass($handler); $reflection->getProperty('workingDir')->setValue($handler, __DIR__ . '/data/handler/'); $method = $reflection->getMethod('getImages'); $result = $method->invoke($handler, ...$args); - + $this->assertSame($expected, $result); } #[DataProvider('updateImagesDataProvider')] - public function testupdateImages(string $expected, array $args): void { + public function testUpdateImages(string $expected, array $args): void { $handler = new Handler('fichier', 'article', 2); $reflection = new ReflectionClass($handler); $method = $reflection->getMethod('updateImages'); - + $result = $method->invoke($handler, ...$args); - + $this->assertSame($expected, $result); } - - public static function identifyImagesDataProvider(): array { + + public static function getImagesDataProvider(): array { return [ 'no image' => [ ["Here comes {{bold}} text\n\n", null], @@ -121,7 +168,7 @@ class HandlerTest extends TestCase ], ]; } - + public static function updateImagesDataProvider(): array { return [ 'id' => [ @@ -192,5 +239,5 @@ class HandlerTest extends TestCase ] ] ]; - } -} \ No newline at end of file + } +} diff --git a/tests/data/conversion.docx b/tests/data/conversion.docx new file mode 100644 index 0000000000000000000000000000000000000000..2ed4d34907ba7ff498a7ace2976f50c4a05dc047 Binary files /dev/null and b/tests/data/conversion.docx differ