|
29 | 29 | * This copyright notice MUST APPEAR in all copies of the script!
|
30 | 30 | */
|
31 | 31 |
|
| 32 | +use In2code\In2publishCore\Communication\RemoteCommandExecution\RemoteCommandDispatcher; |
| 33 | +use In2code\In2publishCore\Communication\RemoteCommandExecution\RemoteCommandRequest; |
| 34 | +use In2code\In2publishCore\Communication\RemoteCommandExecution\RemoteCommandResponse; |
| 35 | +use In2code\In2publishCore\Communication\TemporaryAssetTransmission\AssetTransmitter; |
32 | 36 | use In2code\In2publishCore\Communication\TemporaryAssetTransmission\TransmissionAdapter\AdapterInterface;
|
| 37 | +use In2code\In2publishCore\Testing\Tests\Configuration\ConfigurationFormatTest; |
33 | 38 | use In2code\In2publishCore\Testing\Tests\TestCaseInterface;
|
34 | 39 | use In2code\In2publishCore\Testing\Tests\TestResult;
|
| 40 | +use Throwable; |
| 41 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
35 | 42 |
|
36 | 43 | use function array_merge;
|
| 44 | +use function bin2hex; |
| 45 | +use function file_put_contents; |
| 46 | +use function is_file; |
| 47 | +use function random_bytes; |
| 48 | +use function register_shutdown_function; |
| 49 | +use function strpos; |
| 50 | +use function uniqid; |
| 51 | +use function unlink; |
37 | 52 |
|
38 | 53 | class TransmissionAdapterTest implements TestCaseInterface
|
39 | 54 | {
|
| 55 | + /** @var AssetTransmitter */ |
| 56 | + protected $assetTransmitter; |
| 57 | + |
| 58 | + /** @var RemoteCommandDispatcher */ |
| 59 | + protected $remoteCommandDispatcher; |
| 60 | + |
| 61 | + public function __construct(AssetTransmitter $assetTransmitter, RemoteCommandDispatcher $remoteCommandDispatcher) |
| 62 | + { |
| 63 | + $this->assetTransmitter = $assetTransmitter; |
| 64 | + $this->remoteCommandDispatcher = $remoteCommandDispatcher; |
| 65 | + } |
| 66 | + |
40 | 67 | public function run(): TestResult
|
41 | 68 | {
|
42 |
| - return new TestResult('adapter.all_transmission_adapter_tests__passed'); |
| 69 | + try { |
| 70 | + $canary = bin2hex(random_bytes(16)); |
| 71 | + } catch (Throwable $e) { |
| 72 | + $canary = uniqid('tx_in2publishcore_test_', true); |
| 73 | + } |
| 74 | + $localTmpFile = GeneralUtility::tempnam('tx_in2publishlocal_test_', '.txt'); |
| 75 | + file_put_contents($localTmpFile, $canary); |
| 76 | + register_shutdown_function(static function () use ($localTmpFile) { |
| 77 | + if (is_file($localTmpFile)) { |
| 78 | + unlink($localTmpFile); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + try { |
| 83 | + $foreignTmpFile = $this->assetTransmitter->transmitTemporaryFile($localTmpFile); |
| 84 | + } catch (Throwable $exception) { |
| 85 | + return new TestResult( |
| 86 | + 'LLL:EXT:in2publish_core/Resources/Private/Language/locallang.testing.xlf:adapter.transmission.asset_transmitter_error', |
| 87 | + TestResult::ERROR, |
| 88 | + [(string)$exception] |
| 89 | + ); |
| 90 | + } |
| 91 | + |
| 92 | + GeneralUtility::unlink_tempfile($localTmpFile); |
| 93 | + |
| 94 | + $getContentResponse = $this->getForeignFileContents($foreignTmpFile); |
| 95 | + if (!$getContentResponse->isSuccessful()) { |
| 96 | + $needle = 'cat: ' . $foreignTmpFile . ': No such file or directory'; |
| 97 | + if (false !== strpos($getContentResponse->getErrorsString(), $needle)) { |
| 98 | + return new TestResult( |
| 99 | + 'LLL:EXT:in2publish_core/Resources/Private/Language/locallang.testing.xlf:adapter.transmission.file_not_transferred', |
| 100 | + TestResult::ERROR, |
| 101 | + [$getContentResponse->getOutputString(), $getContentResponse->getErrorsString()] |
| 102 | + ); |
| 103 | + } |
| 104 | + $rmResponse = $this->removeForeignFile($foreignTmpFile); |
| 105 | + if ($rmResponse->isSuccessful()) { |
| 106 | + return new TestResult( |
| 107 | + 'LLL:EXT:in2publish_core/Resources/Private/Language/locallang.testing.xlf:adapter.transmission.file_cat_failed', |
| 108 | + TestResult::ERROR, |
| 109 | + [$getContentResponse->getOutputString(), $getContentResponse->getErrorsString()] |
| 110 | + ); |
| 111 | + } |
| 112 | + return new TestResult( |
| 113 | + 'LLL:EXT:in2publish_core/Resources/Private/Language/locallang.testing.xlf:adapter.transmission.file_cat_failed_manual_remove', |
| 114 | + TestResult::ERROR, |
| 115 | + [ |
| 116 | + $getContentResponse->getOutputString(), |
| 117 | + $getContentResponse->getErrorsString(), |
| 118 | + $rmResponse->getOutputString(), |
| 119 | + $rmResponse->getErrorsString(), |
| 120 | + ], |
| 121 | + [$foreignTmpFile] |
| 122 | + ); |
| 123 | + } |
| 124 | + |
| 125 | + $rmResponse = $this->removeForeignFile($foreignTmpFile); |
| 126 | + |
| 127 | + if ($getContentResponse->getOutputString() !== $canary) { |
| 128 | + if ($rmResponse->isSuccessful()) { |
| 129 | + return new TestResult( |
| 130 | + 'LLL:EXT:in2publish_core/Resources/Private/Language/locallang.testing.xlf:adapter.transmission.content_altered', |
| 131 | + TestResult::ERROR, |
| 132 | + [$getContentResponse->getOutputString(), $getContentResponse->getErrorsString()] |
| 133 | + ); |
| 134 | + } |
| 135 | + return new TestResult( |
| 136 | + 'LLL:EXT:in2publish_core/Resources/Private/Language/locallang.testing.xlf:adapter.transmission.content_altered_manual_remove', |
| 137 | + TestResult::ERROR, |
| 138 | + [ |
| 139 | + 'Expected: "' . $canary . '"', |
| 140 | + 'Actual: "' . $getContentResponse->getOutputString() . '"', |
| 141 | + $getContentResponse->getErrorsString(), |
| 142 | + $rmResponse->getOutputString(), |
| 143 | + $rmResponse->getErrorsString(), |
| 144 | + ], |
| 145 | + [$foreignTmpFile] |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + if (!$rmResponse->isSuccessful()) { |
| 150 | + return new TestResult( |
| 151 | + 'LLL:EXT:in2publish_core/Resources/Private/Language/locallang.testing.xlf:adapter.transmission.file_deletion_failed', |
| 152 | + TestResult::ERROR, |
| 153 | + [$rmResponse->getOutputString(), $rmResponse->getErrorsString()], |
| 154 | + [$foreignTmpFile] |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + return new TestResult( |
| 159 | + 'LLL:EXT:in2publish_core/Resources/Private/Language/locallang.testing.xlf:adapter.transmission.all_tests_passed' |
| 160 | + ); |
| 161 | + } |
| 162 | + |
| 163 | + protected function removeForeignFile(string $foreignTmpFile): RemoteCommandResponse |
| 164 | + { |
| 165 | + $rceRequest = new RemoteCommandRequest('rm', [], [$foreignTmpFile]); |
| 166 | + $rceRequest->setDispatcher(''); |
| 167 | + $rceRequest->usePhp(false); |
| 168 | + |
| 169 | + return $this->remoteCommandDispatcher->dispatch($rceRequest); |
| 170 | + } |
| 171 | + |
| 172 | + protected function getForeignFileContents(string $foreignTmpFile): RemoteCommandResponse |
| 173 | + { |
| 174 | + $rceRequest = new RemoteCommandRequest('cat', [], [$foreignTmpFile]); |
| 175 | + $rceRequest->setDispatcher(''); |
| 176 | + $rceRequest->usePhp(false); |
| 177 | + |
| 178 | + return $this->remoteCommandDispatcher->dispatch($rceRequest); |
43 | 179 | }
|
44 | 180 |
|
45 | 181 | /** @SuppressWarnings(PHPMD.Superglobals) */
|
46 | 182 | public function getDependencies(): array
|
47 | 183 | {
|
48 | 184 | $dependencies = [
|
| 185 | + ConfigurationFormatTest::class, |
49 | 186 | AdapterSelectionTest::class,
|
50 | 187 | ];
|
51 | 188 | if (isset($GLOBALS['in2publish_core']['virtual_tests'][AdapterInterface::class])) {
|
|
0 commit comments