‪TYPO3CMS  ‪main
ImportCommandTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use PHPUnit\Framework\Attributes\DataProvider;
21 use PHPUnit\Framework\Attributes\Test;
22 use Symfony\Component\Console\Exception\RuntimeException;
23 use Symfony\Component\Console\Output\Output;
24 use Symfony\Component\Console\Tester\CommandTester;
28 
30 {
31  #[Test]
32  public function ‪importCommandRequiresFileArgument(): void
33  {
34  $this->expectException(RuntimeException::class);
35  $this->expectExceptionMessage('Not enough arguments (missing: "file")');
36  $tester = new CommandTester(new ‪ImportCommand(new ‪Import()));
37  $tester->execute([], []);
38  }
39 
40  #[Test]
42  {
43  $filePath = 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/sys_news.xml';
44  $tester = new CommandTester(new ‪ImportCommand(new ‪Import()));
45  $tester->execute(['file' => $filePath], []);
46  self::assertEquals(0, $tester->getStatusCode());
47  }
48 
49  #[Test]
51  {
52  $input = [
53  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/sys_news.xml',
54  'pid' => 3,
55  '--update-records' => false,
56  '--ignore-pid' => false,
57  '--force-uid' => false,
58  '--enable-log' => false,
59  '--import-mode' => [
60  sprintf('pages:789=%s', ‪Import::IMPORT_MODE_FORCE_UID),
61  sprintf('tt_content:1=%s', ‪Import::IMPORT_MODE_EXCLUDE),
62  ],
63  ];
64 
65  $importMock = $this->getAccessibleMock(Import::class, [
66  'setPid', 'setUpdate', 'setGlobalIgnorePid', 'setForceAllUids', 'setEnableLogging', 'loadFile',
67  'setImportMode',
68  ]);
69 
70  $importMock->expects(self::once())->method('setPid')->with(self::equalTo(3));
71  $importMock->expects(self::once())->method('setUpdate')->with(self::equalTo(false));
72  $importMock->expects(self::once())->method('setGlobalIgnorePid')->with(self::equalTo(false));
73  $importMock->expects(self::once())->method('setForceAllUids')->with(self::equalTo(false));
74  $importMock->expects(self::once())->method('setEnableLogging')->with(self::equalTo(false));
75  $importMock->expects(self::once())->method('setImportMode')->with(self::equalTo([
76  'tt_content:1' => ‪Import::IMPORT_MODE_EXCLUDE,
77  'pages:789' => ‪Import::IMPORT_MODE_FORCE_UID,
78  ]));
79  $importMock->expects(self::once())->method('loadFile')->with(self::equalTo('EXT:impexp/Tests/Functional/Fixtures/XmlImports/sys_news.xml'));
80 
81  $tester = new CommandTester(new ‪ImportCommand($importMock));
82  $tester->execute($input);
83  }
84 
85  public static function ‪importCommandFailsDataProvider(): array
86  {
87  return [
88  'path to not existing file' => [
89  [
90  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/me_does_not_exist.xml',
91  '--force-uid' => true,
92  ],
93  'expected' => 'File not found: ',
94  ],
95  'unsupported file extension' => [
96  [
97  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/unsupported.json',
98  '--force-uid' => true,
99  ],
100  'expected' => 'File extension "json" is not valid. Supported file extensions are "xml", "t3d".',
101  ],
102  'missing required extension' => [
103  [
104  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/sys_category_table_with_news.xml',
105  '--force-uid' => true,
106  ],
107  'expected' => 'Prerequisites for file import are not met.',
108  ],
109  'missing required storage path' => [
110  [
111  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/pages-and-ttcontent-with-image-with-invalid-storage.xml',
112  '--force-uid' => true,
113  ],
114  'expected' => 'Prerequisites for file import are not met.',
115  ],
116  'forcing uids of sys_file records not supported' => [
117  [
118  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/pages-and-ttcontent-with-image-with-forced-uids.xml',
119  '--force-uid' => true,
120  ],
121  'expected' => 'The import has failed.',
122  ],
123  'import mode does not match associative array pattern of cli' => [
124  [
125  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/pages-and-ttcontent.xml',
126  '--import-mode' => [sprintf('pages:987:%s', ‪Import::IMPORT_MODE_FORCE_UID)],
127  ],
128  'expected' => sprintf('Command line option "import-mode" has invalid entry "pages:987:%s".', ‪Import::IMPORT_MODE_FORCE_UID),
129  ],
130  ];
131  }
132 
133  #[DataProvider('importCommandFailsDataProvider')]
134  #[Test]
135  public function ‪importCommandFails(array $input, string $expected): void
136  {
137  $tester = new CommandTester(new ‪ImportCommand(new ‪Import()));
138  $tester->execute(
139  $input,
140  ['verbosity' => Output::VERBOSITY_VERBOSE]
141  );
142 
143  self::assertEquals(1, $tester->getStatusCode());
144  self::assertStringContainsString($expected, $tester->getDisplay(true));
145  }
146 }
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest
Definition: ImportCommandTest.php:30
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest\importCommandRequiresFileArgumentOnly
‪importCommandRequiresFileArgumentOnly()
Definition: ImportCommandTest.php:41
‪TYPO3\CMS\Impexp\Command\ImportCommand
Definition: ImportCommand.php:36
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest\importCommandFails
‪importCommandFails(array $input, string $expected)
Definition: ImportCommandTest.php:135
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest\importCommandPassesArgumentsToImportObject
‪importCommandPassesArgumentsToImportObject()
Definition: ImportCommandTest.php:50
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest\importCommandRequiresFileArgument
‪importCommandRequiresFileArgument()
Definition: ImportCommandTest.php:32
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_FORCE_UID
‪const IMPORT_MODE_FORCE_UID
Definition: Import.php:52
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_EXCLUDE
‪const IMPORT_MODE_EXCLUDE
Definition: Import.php:54
‪TYPO3\CMS\Impexp\Import
Definition: Import.php:51
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest\importCommandFailsDataProvider
‪static importCommandFailsDataProvider()
Definition: ImportCommandTest.php:85
‪TYPO3\CMS\Impexp\Tests\Functional\AbstractImportExportTestCase
Definition: AbstractImportExportTestCase.php:33
‪TYPO3\CMS\Impexp\Tests\Functional\Command
Definition: ExportCommandTest.php:18