[FEATURE] EXT:form - integrate new form framework
[Packages/TYPO3.CMS.git] / typo3 / sysext / form / Tests / Unit / Mvc / ProcessingRuleTest.php
1 <?php
2 namespace TYPO3\CMS\Form\Tests\Unit\Mvc\Configuration;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Tests\UnitTestCase;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\Extbase\Error\Result;
20 use TYPO3\CMS\Extbase\Object\ObjectManager;
21 use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
22 use TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator;
23 use TYPO3\CMS\Form\Mvc\ProcessingRule;
24 use TYPO3\CMS\Form\Tests\Unit\Mvc\Validation\Fixtures\TestValidator;
25
26 /**
27 * Test case
28 */
29 class ProcessingRuleTest extends UnitTestCase
30 {
31
32 /**
33 * @var array A backup of registered singleton instances
34 */
35 protected $singletonInstances = [];
36
37 /**
38 * Set up
39 */
40 public function setUp()
41 {
42 $this->singletonInstances = GeneralUtility::getSingletonInstances();
43 }
44
45 /**
46 * Tear down
47 */
48 public function tearDown()
49 {
50 GeneralUtility::resetSingletonInstances($this->singletonInstances);
51 parent::tearDown();
52 }
53
54 /**
55 * @test
56 */
57 public function addValidatorAddValidator()
58 {
59 $mockProcessingRule = $this->getAccessibleMock(ProcessingRule::class, [
60 'dummy'
61 ], [], '', false);
62
63 $mockProcessingRule->_set('validator', new ConjunctionValidator([]));
64 $mockProcessingRule->addValidator(new TestValidator());
65 $validators = $mockProcessingRule->_get('validator')->getValidators();
66 $validators->rewind();
67 $this->assertInstanceOf(AbstractValidator::class, $validators->current());
68 }
69
70 /**
71 * @test
72 */
73 public function processNoPropertyMappingReturnsNotModifiedValue()
74 {
75 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
76 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
77 $resultProphecy = $this->prophesize(Result::class);
78
79 $objectMangerProphecy
80 ->get(Result::class)
81 ->willReturn($resultProphecy->reveal());
82
83 $mockProcessingRule = $this->getAccessibleMock(ProcessingRule::class, [
84 'dummy'
85 ], [], '', false);
86
87 $mockProcessingRule->_set('dataType', null);
88 $mockProcessingRule->_set('processingMessages', $resultProphecy->reveal());
89 $mockProcessingRule->_set('validator', new ConjunctionValidator([]));
90
91 $input = 'someValue';
92 $this->assertSame($input, $mockProcessingRule->_call('process', $input));
93 }
94
95 /**
96 * @test
97 */
98 public function processNoPropertyMappingAndHasErrorsIfValidatorContainsErrors()
99 {
100 $objectMangerProphecy = $this->prophesize(ObjectManager::class);
101 GeneralUtility::setSingletonInstance(ObjectManager::class, $objectMangerProphecy->reveal());
102
103 $objectMangerProphecy
104 ->get(Result::class)
105 ->willReturn(new Result);
106
107 $mockProcessingRule = $this->getAccessibleMock(ProcessingRule::class, [
108 'dummy'
109 ], [], '', true);
110
111 $mockProcessingRule->_set('dataType', null);
112 $mockProcessingRule->_set('validator', new ConjunctionValidator([]));
113 $mockProcessingRule->addValidator(new TestValidator());
114
115 $input = 'addError';
116 $mockProcessingRule->_call('process', $input);
117
118 $this->assertTrue($mockProcessingRule->_get('processingMessages')->hasErrors());
119 }
120 }