|
| 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 | + |
| 18 | +namespace TYPO3\CMS\Core\Tests\Functional\DataHandling\DataHandler; |
| 19 | + |
| 20 | +use PHPUnit\Framework\Attributes\Test; |
| 21 | +use TYPO3\CMS\Core\Authentication\BackendUserAuthentication; |
| 22 | +use TYPO3\CMS\Core\DataHandling\DataHandler; |
| 23 | +use TYPO3\CMS\Core\Schema\Capability\TcaSchemaCapability; |
| 24 | +use TYPO3\CMS\Core\Schema\TcaSchemaFactory; |
| 25 | +use TYPO3\CMS\Core\Type\Bitmask\Permission; |
| 26 | +use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; |
| 27 | + |
| 28 | +final class UndeleteRecordTest extends FunctionalTestCase |
| 29 | +{ |
| 30 | + protected array $coreExtensionsToLoad = ['workspaces']; |
| 31 | + private BackendUserAuthentication $backendUser; |
| 32 | + private DataHandler $subject; |
| 33 | + |
| 34 | + protected function setUp(): void |
| 35 | + { |
| 36 | + parent::setUp(); |
| 37 | + |
| 38 | + $this->importCSVDataSet(dirname(__DIR__, 2) . '/Fixtures/be_groups.csv'); |
| 39 | + $this->importCSVDataSet(dirname(__DIR__, 2) . '/Fixtures/be_users.csv'); |
| 40 | + $this->importCSVDataSet(dirname(__DIR__, 2) . '/Fixtures/pages.csv'); |
| 41 | + |
| 42 | + $this->backendUser = $this->setUpBackendUser(9); |
| 43 | + // allow modifying the live workspace |
| 44 | + $this->backendUser->groupData['workspace_perms'] = 1; |
| 45 | + $this->backendUser->setWorkspace(0); |
| 46 | + $this->backendUser->setWebmounts([1]); |
| 47 | + |
| 48 | + $this->subject = $this->get(DataHandler::class); |
| 49 | + } |
| 50 | + |
| 51 | + #[Test] |
| 52 | + public function undeleteWorksAsAnEditor(): void |
| 53 | + { |
| 54 | + $this->subject->start([], []); |
| 55 | + $this->subject->deleteAction('pages', 10); |
| 56 | + self::assertTrue($this->databaseRecordExists('pages', 10, true)); |
| 57 | + |
| 58 | + $cmd = [ |
| 59 | + 'pages' => [ |
| 60 | + 10 => [ |
| 61 | + 'undelete' => 1, |
| 62 | + ], |
| 63 | + ], |
| 64 | + ]; |
| 65 | + |
| 66 | + $this->subject->start([], $cmd); |
| 67 | + $this->subject->process_cmdmap(); |
| 68 | + self::assertTrue($this->databaseRecordExists('pages', 10, false)); |
| 69 | + } |
| 70 | + |
| 71 | + #[Test] |
| 72 | + public function undeleteIsProhibitedIfMissingWritePermissionToParentPageAsAnEditor(): void |
| 73 | + { |
| 74 | + $this->subject->start([], []); |
| 75 | + $this->subject->deleteAction('pages', 10); |
| 76 | + self::assertTrue($this->databaseRecordExists('pages', 10, true)); |
| 77 | + |
| 78 | + $this->getConnectionPool() |
| 79 | + ->getConnectionForTable('pages') |
| 80 | + ->update( |
| 81 | + 'pages', |
| 82 | + // deny new page creation on page with uid 4 (page 10 has pid 4) |
| 83 | + ['perms_everybody' => Permission::ALL & ~Permission::PAGE_NEW], |
| 84 | + ['uid' => 4] |
| 85 | + ); |
| 86 | + |
| 87 | + $cmd = [ |
| 88 | + 'pages' => [ |
| 89 | + 10 => [ |
| 90 | + 'undelete' => 1, |
| 91 | + ], |
| 92 | + ], |
| 93 | + ]; |
| 94 | + |
| 95 | + $this->subject->start([], $cmd); |
| 96 | + $this->subject->process_cmdmap(); |
| 97 | + |
| 98 | + // Page must not have been deleted |
| 99 | + self::assertTrue($this->databaseRecordExists('pages', 10, true)); |
| 100 | + $this->assertLogEntry('Record "pages:10" can\'t be restored: Insufficient user permissions to target page 4', 'pages', 10); |
| 101 | + } |
| 102 | + |
| 103 | + #[Test] |
| 104 | + public function undeleteIsProhibitedIfMissingTablePermissionsAsAnEditor(): void |
| 105 | + { |
| 106 | + $this->subject->start([], []); |
| 107 | + $this->subject->deleteAction('pages', 10); |
| 108 | + self::assertTrue($this->databaseRecordExists('pages', 10, true)); |
| 109 | + |
| 110 | + $this->getConnectionPool() |
| 111 | + ->getConnectionForTable('be_groups') |
| 112 | + ->update( |
| 113 | + 'be_groups', |
| 114 | + // deny new page modification |
| 115 | + ['tables_modify' => 'tt_content'], |
| 116 | + ['uid' => 9] |
| 117 | + ); |
| 118 | + |
| 119 | + // Reload backend user after changes to the user group |
| 120 | + $this->backendUser = $this->setUpBackendUser(9); |
| 121 | + |
| 122 | + $cmd = [ |
| 123 | + 'pages' => [ |
| 124 | + 10 => [ |
| 125 | + 'undelete' => 1, |
| 126 | + ], |
| 127 | + ], |
| 128 | + ]; |
| 129 | + |
| 130 | + $this->subject->start([], $cmd); |
| 131 | + $this->subject->process_cmdmap(); |
| 132 | + |
| 133 | + // Page must not have been deleted |
| 134 | + self::assertTrue($this->databaseRecordExists('pages', 10, true)); |
| 135 | + $this->assertLogEntry('Attempt to modify table "pages" without permission'); |
| 136 | + } |
| 137 | + |
| 138 | + private function databaseRecordExists(string $tableName, int $id, ?bool $expectDeleted): bool |
| 139 | + { |
| 140 | + $schema = $this->get(TcaSchemaFactory::class)->get($tableName); |
| 141 | + $softDeleteFieldName = $schema->hasCapability(TcaSchemaCapability::SoftDelete) |
| 142 | + ? $schema->getCapability(TcaSchemaCapability::SoftDelete)->getFieldName() |
| 143 | + : null; |
| 144 | + |
| 145 | + $identifiers = ['uid' => $id]; |
| 146 | + if ($expectDeleted !== null && $softDeleteFieldName !== null) { |
| 147 | + $identifiers[$softDeleteFieldName] = (int)$expectDeleted; |
| 148 | + } |
| 149 | + |
| 150 | + $queryBuilder = $this->getConnectionPool() |
| 151 | + ->getQueryBuilderForTable($tableName) |
| 152 | + ->count('uid') |
| 153 | + ->from($tableName); |
| 154 | + $queryBuilder->getRestrictions()->removeAll(); |
| 155 | + foreach ($identifiers as $identifier => $value) { |
| 156 | + $queryBuilder->andWhere($queryBuilder->expr()->eq($identifier, $queryBuilder->createNamedParameter($value))); |
| 157 | + } |
| 158 | + return (int)$queryBuilder->executeQuery()->fetchOne() === 1; |
| 159 | + } |
| 160 | + |
| 161 | + private function assertLogEntry(string $logTemplate, ?string $tableName = null, ?int $id = null): void |
| 162 | + { |
| 163 | + $text = sprintf($logTemplate, (string)$tableName, $id !== null ? (string)$id : ''); |
| 164 | + $matches = array_filter( |
| 165 | + $this->subject->errorLog, |
| 166 | + static fn(string $entry): bool => str_ends_with($entry, $text) |
| 167 | + ); |
| 168 | + self::assertNotSame([], $matches, 'Unable to find log entry: ' . $text); |
| 169 | + } |
| 170 | +} |
0 commit comments