‪TYPO3CMS  11.5
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 Symfony\Component\Console\Output\Output;
21 use Symfony\Component\Console\Tester\CommandTester;
25 
27 {
31  public function ‪importCommandRequiresFileArgument(): void
32  {
33  $this->expectException(\Symfony\Component\Console\‪Exception\RuntimeException::class);
34  $this->expectExceptionMessage('Not enough arguments (missing: "file")');
35  $tester = new CommandTester(new ‪ImportCommand(new ‪Import()));
36  $tester->execute([], []);
37  }
38 
43  {
44  $filePath = 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/sys_language.xml';
45  $tester = new CommandTester(new ‪ImportCommand(new ‪Import()));
46  $tester->execute(['file' => $filePath], []);
47  self::assertEquals(0, $tester->getStatusCode());
48  }
49 
54  {
55  $input = [
56  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/sys_language.xml',
57  'pid' => 3,
58  // @deprecated since v11, will be removed in v12. Drop the lowerCamelCase options.
59  '--update-records' => false,
60  '--ignore-pid' => false,
61  '--force-uid' => false,
62  '--enable-log' => false,
63  '--import-mode' => [
64  sprintf('pages:789=%s', ‪Import::IMPORT_MODE_FORCE_UID),
65  sprintf('tt_content:1=%s', ‪Import::IMPORT_MODE_EXCLUDE),
66  ],
67  ];
68 
69  $importMock = $this->getAccessibleMock(Import::class, [
70  'setPid', 'setUpdate', 'setGlobalIgnorePid', 'setForceAllUids', 'setEnableLogging', 'loadFile',
71  'setImportMode',
72  ]);
73 
74  $importMock->expects(self::once())->method('setPid')->with(self::equalTo(3));
75  $importMock->expects(self::once())->method('setUpdate')->with(self::equalTo(false));
76  $importMock->expects(self::once())->method('setGlobalIgnorePid')->with(self::equalTo(false));
77  $importMock->expects(self::once())->method('setForceAllUids')->with(self::equalTo(false));
78  $importMock->expects(self::once())->method('setEnableLogging')->with(self::equalTo(false));
79  $importMock->expects(self::once())->method('setImportMode')->with(self::equalTo([
80  'tt_content:1' => ‪Import::IMPORT_MODE_EXCLUDE,
81  'pages:789' => ‪Import::IMPORT_MODE_FORCE_UID,
82  ]));
83  $importMock->expects(self::once())->method('loadFile')->with(self::equalTo('EXT:impexp/Tests/Functional/Fixtures/XmlImports/sys_language.xml'));
84 
85  $tester = new CommandTester(new ‪ImportCommand($importMock));
86  $tester->execute($input);
87  }
88 
89  public function ‪importCommandFailsDataProvider(): array
90  {
91  return [
92  'path to not existing file' => [
93  [
94  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/me_does_not_exist.xml',
95  '--force-uid' => true,
96  ],
97  'expected' => 'File not found: ',
98  ],
99  'unsupported file extension' => [
100  [
101  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/unsupported.json',
102  '--force-uid' => true,
103  ],
104  'expected' => 'File extension "json" is not valid. Supported file extensions are "xml", "t3d".',
105  ],
106  'missing required extension' => [
107  [
108  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/sys_category_table_with_news.xml',
109  '--force-uid' => true,
110  ],
111  'expected' => 'Prerequisites for file import are not met.',
112  ],
113  'missing required storage path' => [
114  [
115  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/pages-and-ttcontent-with-image-with-invalid-storage.xml',
116  '--force-uid' => true,
117  ],
118  'expected' => 'Prerequisites for file import are not met.',
119  ],
120  'forcing uids of sys_file records not supported' => [
121  [
122  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/pages-and-ttcontent-with-image-with-forced-uids.xml',
123  '--force-uid' => true,
124  ],
125  'expected' => 'The import has failed.',
126  ],
127  'import mode does not match associative array pattern of cli' => [
128  [
129  'file' => 'EXT:impexp/Tests/Functional/Fixtures/XmlImports/pages-and-ttcontent.xml',
130  '--import-mode' => [sprintf('pages:987:%s', ‪Import::IMPORT_MODE_FORCE_UID)],
131  ],
132  'expected' => sprintf('Command line option "import-mode" has invalid entry "pages:987:%s".', ‪Import::IMPORT_MODE_FORCE_UID),
133  ],
134  ];
135  }
136 
141  public function ‪importCommandFails(array $input, string $expected): void
142  {
143  $tester = new CommandTester(new ‪ImportCommand(new ‪Import()));
144  $tester->execute(
145  $input,
146  ['verbosity' => Output::VERBOSITY_VERBOSE]
147  );
148 
149  self::assertEquals(1, $tester->getStatusCode());
150  self::assertStringContainsString($expected, $tester->getDisplay(true));
151  }
152 }
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest
Definition: ImportCommandTest.php:27
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest\importCommandRequiresFileArgumentOnly
‪importCommandRequiresFileArgumentOnly()
Definition: ImportCommandTest.php:42
‪TYPO3\CMS\Core\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Impexp\Command\ImportCommand
Definition: ImportCommand.php:34
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest\importCommandFails
‪importCommandFails(array $input, string $expected)
Definition: ImportCommandTest.php:141
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest\importCommandPassesArgumentsToImportObject
‪importCommandPassesArgumentsToImportObject()
Definition: ImportCommandTest.php:53
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest\importCommandRequiresFileArgument
‪importCommandRequiresFileArgument()
Definition: ImportCommandTest.php:31
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_FORCE_UID
‪const IMPORT_MODE_FORCE_UID
Definition: Import.php:51
‪TYPO3\CMS\Impexp\Import\IMPORT_MODE_EXCLUDE
‪const IMPORT_MODE_EXCLUDE
Definition: Import.php:53
‪TYPO3\CMS\Impexp\Tests\Functional\Command\ImportCommandTest\importCommandFailsDataProvider
‪importCommandFailsDataProvider()
Definition: ImportCommandTest.php:89
‪TYPO3\CMS\Impexp\Import
Definition: Import.php:50
‪TYPO3\CMS\Impexp\Tests\Functional\AbstractImportExportTestCase
Definition: AbstractImportExportTestCase.php:34
‪TYPO3\CMS\Impexp\Tests\Functional\Command
Definition: ExportCommandTest.php:18