[FEATURE] Allow deactivation of FormDataProviders
[Packages/TYPO3.CMS.git] / typo3 / sysext / backend / Tests / Unit / Form / FormDataGroup / OrderedProviderListTest.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Backend\Tests\Unit\Form\FormDataGroup;
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 use Prophecy\Argument;
19 use Prophecy\Prophecy\ObjectProphecy;
20 use TYPO3\CMS\Backend\Form\FormDataGroup\OrderedProviderList;
21 use TYPO3\CMS\Backend\Form\FormDataProviderInterface;
22 use TYPO3\CMS\Core\Service\DependencyOrderingService;
23 use TYPO3\CMS\Core\Utility\GeneralUtility;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25
26 /**
27 * Test case
28 */
29 class OrderedProviderListTest extends UnitTestCase
30 {
31 /**
32 * @test
33 */
34 public function compileReturnsIncomingData()
35 {
36 /** @var DependencyOrderingService|ObjectProphecy $orderingServiceProphecy */
37 $orderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
38 GeneralUtility::addInstance(DependencyOrderingService::class, $orderingServiceProphecy->reveal());
39 $orderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
40
41 $input = ['foo'];
42
43 $subject = new OrderedProviderList();
44 $subject->setProviderList([]);
45 $this->assertEquals($input, $subject->compile($input));
46 }
47
48 /**
49 * @test
50 */
51 public function compileReturnsResultChangedByDataProvider()
52 {
53 /** @var DependencyOrderingService|ObjectProphecy $orderingServiceProphecy */
54 $orderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
55 GeneralUtility::addInstance(DependencyOrderingService::class, $orderingServiceProphecy->reveal());
56 $orderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
57
58 /** @var FormDataProviderInterface|ObjectProphecy $formDataProviderProphecy */
59 $formDataProviderProphecy = $this->prophesize(FormDataProviderInterface::class);
60 GeneralUtility::addInstance(FormDataProviderInterface::class, $formDataProviderProphecy->reveal());
61 $providerResult = ['foo'];
62 $formDataProviderProphecy->addData(Argument::cetera())->shouldBeCalled()->willReturn($providerResult);
63
64 $subject = new OrderedProviderList();
65 $subject->setProviderList([
66 FormDataProviderInterface::class => [],
67 ]);
68 $this->assertEquals($providerResult, $subject->compile([]));
69 }
70
71 /**
72 * @test
73 */
74 public function compileDoesNotCallDisabledDataProvider()
75 {
76 /** @var DependencyOrderingService|ObjectProphecy $orderingServiceProphecy */
77 $orderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
78 GeneralUtility::addInstance(DependencyOrderingService::class, $orderingServiceProphecy->reveal());
79 $orderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
80
81 $subject = new OrderedProviderList();
82 $subject->setProviderList([
83 FormDataProviderInterface::class => [
84 'disabled' => true,
85 ],
86 ]);
87 $input = ['foo'];
88 $this->assertEquals($input, $subject->compile($input));
89 }
90
91 /**
92 * @test
93 */
94 public function compileThrowsExceptionIfDataProviderDoesNotImplementInterface()
95 {
96 /** @var DependencyOrderingService|ObjectProphecy $orderingServiceProphecy */
97 $orderingServiceProphecy = $this->prophesize(DependencyOrderingService::class);
98 GeneralUtility::addInstance(DependencyOrderingService::class, $orderingServiceProphecy->reveal());
99 $orderingServiceProphecy->orderByDependencies(Argument::cetera())->willReturnArgument(0);
100
101 /** @var FormDataProviderInterface|ObjectProphecy $formDataProviderProphecy */
102 $formDataProviderProphecy = $this->prophesize(\stdClass::class);
103 GeneralUtility::addInstance(\stdClass::class, $formDataProviderProphecy->reveal());
104
105 $this->expectException(\UnexpectedValueException::class);
106 $this->expectExceptionCode(1485299408);
107
108 $subject = new OrderedProviderList();
109 $subject->setProviderList([
110 \stdClass::class => [],
111 ]);
112 $subject->compile([]);
113 }
114 }