‪TYPO3CMS  10.4
WorkspaceTest.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 
30 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
31 
32 class ‪WorkspaceTest extends FunctionalTestCase
33 {
37  protected ‪$testExtensionsToLoad = [
38  'typo3/sysext/extbase/Tests/Functional/Fixtures/Extensions/blog_example',
39  ];
40 
44  protected ‪$coreExtensionsToLoad = [
45  'extbase',
46  'fluid',
47  'workspaces',
48  ];
49 
53  protected ‪$blogRepository;
54 
58  protected function ‪setUp(): void
59  {
60  parent::setUp();
61  $this->importDataSet('PACKAGE:typo3/testing-framework/Resources/Core/Functional/Fixtures/pages.xml');
62  $this->importDataSet('EXT:extbase/Tests/Functional/Persistence/Fixtures/blogs.xml');
63  $this->importDataSet('EXT:extbase/Tests/Functional/Persistence/Fixtures/posts.xml');
64  $this->importDataSet('EXT:extbase/Tests/Functional/Persistence/Fixtures/categories.xml');
65  $this->importDataSet('EXT:extbase/Tests/Functional/Persistence/Fixtures/category-mm.xml');
66  }
67 
68  protected function ‪tearDown(): void
69  {
70  // Reset to TYPO3_MODE = BE as defined in functional tests
71  GeneralUtility::makeInstance(EnvironmentService::class)->setFrontendMode(false);
72  parent::tearDown(); // TODO: Change the autogenerated stub
73  }
74 
75  public function ‪contextDataProvider(): array
76  {
77  return [
78  'test frontend context' => [
79  'context' => 'FE',
80  ],
81  'test backend context' => [
82  'context' => 'BE',
83  ],
84  ];
85  }
86 
92  public function ‪countReturnsCorrectNumberOfBlogs(string $context)
93  {
94  if ($context === 'FE') {
96  } else {
97  $this->‪setupSubjectInBackend();
98  }
99 
100  $query = $this->blogRepository->createQuery();
101 
102  $querySettings = $query->getQuerySettings();
103  $querySettings->setRespectStoragePage(false);
104 
105  // In workspace all records need to be fetched, thus enableFields is ignored
106  // This means we select even hidden (but not deleted) records for count()
107  self::assertSame(5, $query->execute()->count());
108  }
109 
115  public function ‪fetchingAllBlogsReturnsCorrectNumberOfBlogs(string $context)
116  {
117  if ($context === 'FE') {
118  $this->‪setupSubjectInFrontend();
119  } else {
120  $this->‪setupSubjectInBackend();
121  }
122 
123  $query = $this->blogRepository->createQuery();
124 
125  $querySettings = $query->getQuerySettings();
126  $querySettings->setRespectStoragePage(false);
127 
128  $query->setOrderings(['uid' => ‪QueryInterface::ORDER_ASCENDING]);
129 
130  $blogs = $query->execute()->toArray();
131 
132  self::assertSame(4, count($blogs));
133 
134  // Check first blog was overlaid with workspace preview
135  $firstBlog = array_shift($blogs);
136  self::assertSame(1, $firstBlog->getUid());
137  self::assertSame('WorkspaceOverlay Blog1', $firstBlog->getTitle());
138 
139  // Check last blog was enabled in workspace preview
140  $lastBlog = array_pop($blogs);
141  self::assertSame(6, $lastBlog->getUid());
142  self::assertSame('WorkspaceOverlay Blog6Enabled', $lastBlog->getTitle());
143  }
144 
150  public function ‪fetchingBlogReturnsOverlaidWorkspaceVersionForRelations(string $context)
151  {
152  if ($context === 'FE') {
153  $this->‪setupSubjectInFrontend();
154  } else {
155  $this->‪setupSubjectInBackend();
156  }
157 
158  $query = $this->blogRepository->createQuery();
159 
160  $querySettings = $query->getQuerySettings();
161  $querySettings->setStoragePageIds([0]);
162 
163  $query->matching($query->equals('uid', 1));
164 
165  $blog = $query->execute()->getFirst();
166  $posts = $blog->getPosts()->toArray();
167 
168  self::assertSame('WorkspaceOverlay Blog1', $blog->getTitle());
169  self::assertCount(10, (array)$posts);
170  self::assertSame('WorkspaceOverlay Post1', $posts[0]->getTitle());
171  self::assertSame('WorkspaceOverlay Post2', $posts[1]->getTitle());
172  self::assertSame('WorkspaceOverlay Post3', $posts[2]->getTitle());
173  }
174 
179  {
180  // Simulate LIVE workspace -> 3 relations
181  $this->‪setupSubjectInFrontend(0);
182  $query = $this->blogRepository->createQuery();
183  $querySettings = $query->getQuerySettings();
184  $querySettings->setRespectStoragePage(false);
185  $query->matching($query->equals('uid', 1));
186 
188  $blog = $query->execute()->getFirst();
189  self::assertEquals('Blog1', $blog->getTitle());
190  self::assertCount(3, $blog->getCategories());
191  }
192 
197  {
198  $this->‪setupSubjectInFrontend(1);
199  $query = $this->blogRepository->createQuery();
200  $querySettings = $query->getQuerySettings();
201  $querySettings->setRespectStoragePage(false);
202  $query->matching($query->equals('uid', 1));
203 
205  $blog = $query->execute()->getFirst();
206  self::assertEquals('WorkspaceOverlay Blog1', $blog->getTitle());
207  self::assertCount(2, $blog->getCategories());
208  }
209 
213  protected function ‪setupSubjectInFrontend(int $workspaceId = 1)
214  {
215  $context = new Context(
216  [
217  'workspace' => new WorkspaceAspect($workspaceId),
218  ]
219  );
220  GeneralUtility::setSingletonInstance(Context::class, $context);
221  GeneralUtility::makeInstance(EnvironmentService::class)->setFrontendMode(true);
222 
223  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
224  $this->blogRepository = $objectManager->get(BlogRepository::class);
225  }
226 
230  protected function ‪setupSubjectInBackend(int $workspaceId = 1)
231  {
232  $backendUser = new BackendUserAuthentication();
233  $backendUser->workspace = $workspaceId;
234  ‪$GLOBALS['BE_USER'] = $backendUser;
235  $context = new Context(
236  [
237  'backend.user' => new UserAspect($backendUser),
238  'workspace' => new WorkspaceAspect($workspaceId),
239  ]
240  );
241  GeneralUtility::setSingletonInstance(Context::class, $context);
242 
243  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
244  $this->blogRepository = $objectManager->get(BlogRepository::class);
245  }
246 }
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\$blogRepository
‪BlogRepository $blogRepository
Definition: WorkspaceTest.php:50
‪ExtbaseTeam\BlogExample\Domain\Repository\BlogRepository
Definition: BlogRepository.php:25
‪TYPO3\CMS\Core\Context\WorkspaceAspect
Definition: WorkspaceAspect.php:31
‪TYPO3\CMS\Extbase\Persistence\QueryInterface
Definition: QueryInterface.php:29
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\tearDown
‪tearDown()
Definition: WorkspaceTest.php:65
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\$testExtensionsToLoad
‪array $testExtensionsToLoad
Definition: WorkspaceTest.php:36
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\$coreExtensionsToLoad
‪array $coreExtensionsToLoad
Definition: WorkspaceTest.php:42
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest
Definition: WorkspaceTest.php:33
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\countReturnsCorrectNumberOfBlogs
‪countReturnsCorrectNumberOfBlogs(string $context)
Definition: WorkspaceTest.php:89
‪TYPO3\CMS\Extbase\Persistence\QueryInterface\ORDER_ASCENDING
‪const ORDER_ASCENDING
Definition: QueryInterface.php:98
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\fetchingBlogReturnsOverlaidWorkspaceVersionForManyToManyRelations
‪fetchingBlogReturnsOverlaidWorkspaceVersionForManyToManyRelations()
Definition: WorkspaceTest.php:193
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\fetchingBlogReturnsOverlaidWorkspaceVersionForRelations
‪fetchingBlogReturnsOverlaidWorkspaceVersionForRelations(string $context)
Definition: WorkspaceTest.php:147
‪ExtbaseTeam\BlogExample\Domain\Model\Blog
Definition: Blog.php:27
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\setupSubjectInBackend
‪setupSubjectInBackend(int $workspaceId=1)
Definition: WorkspaceTest.php:227
‪TYPO3\CMS\Core\Authentication\BackendUserAuthentication
Definition: BackendUserAuthentication.php:62
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence
Definition: AddTest.php:16
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\contextDataProvider
‪contextDataProvider()
Definition: WorkspaceTest.php:72
‪TYPO3\CMS\Extbase\Service\EnvironmentService
Definition: EnvironmentService.php:27
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\fetchingBlogReturnsManyToManyRelationsInLiveWorkspace
‪fetchingBlogReturnsManyToManyRelationsInLiveWorkspace()
Definition: WorkspaceTest.php:175
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\fetchingAllBlogsReturnsCorrectNumberOfBlogs
‪fetchingAllBlogsReturnsCorrectNumberOfBlogs(string $context)
Definition: WorkspaceTest.php:112
‪TYPO3\CMS\Extbase\Object\ObjectManager
Definition: ObjectManager.php:28
‪TYPO3\CMS\Core\Context\UserAspect
Definition: UserAspect.php:38
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\setupSubjectInFrontend
‪setupSubjectInFrontend(int $workspaceId=1)
Definition: WorkspaceTest.php:210
‪TYPO3\CMS\Extbase\Tests\Functional\Persistence\WorkspaceTest\setUp
‪setUp()
Definition: WorkspaceTest.php:55