16
16
use Magento \Catalog \Controller \Adminhtml \Product \NewAction ;
17
17
use Magento \Catalog \Model \Product ;
18
18
use Magento \Catalog \Test \Unit \Controller \Adminhtml \ProductTest ;
19
+ use Magento \Framework \RegexValidator ;
20
+ use Magento \Framework \Validator \Regex ;
21
+ use Magento \Framework \Validator \RegexFactory ;
19
22
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
20
23
use Magento \Framework \View \Result \PageFactory ;
21
24
use PHPUnit \Framework \MockObject \MockObject ;
@@ -42,6 +45,26 @@ class NewActionTest extends ProductTest
42
45
*/
43
46
protected $ initializationHelper ;
44
47
48
+ /**
49
+ * @var RegexValidator|MockObject
50
+ */
51
+ private $ regexValidator ;
52
+
53
+ /**
54
+ * @var RegexFactory
55
+ */
56
+ private $ regexValidatorFactoryMock ;
57
+
58
+ /**
59
+ * @var Regex|MockObject
60
+ */
61
+ private $ regexValidatorMock ;
62
+
63
+ /**
64
+ * @var ForwardFactory&MockObject|MockObject
65
+ */
66
+ private $ resultForwardFactory ;
67
+
45
68
protected function setUp (): void
46
69
{
47
70
$ this ->productBuilder = $ this ->createPartialMock (
@@ -63,37 +86,78 @@ protected function setUp(): void
63
86
->disableOriginalConstructor ()
64
87
->setMethods (['create ' ])
65
88
->getMock ();
66
- $ resultPageFactory ->expects ($ this ->atLeastOnce ())
67
- ->method ('create ' )
68
- ->willReturn ($ this ->resultPage );
69
89
70
90
$ this ->resultForward = $ this ->getMockBuilder (Forward::class)
71
91
->disableOriginalConstructor ()
72
92
->getMock ();
73
- $ resultForwardFactory = $ this ->getMockBuilder (ForwardFactory::class)
93
+ $ this ->resultForwardFactory = $ this ->getMockBuilder (ForwardFactory::class)
94
+ ->disableOriginalConstructor ()
95
+ ->onlyMethods (['create ' ])
96
+ ->getMock ();
97
+
98
+ $ this ->regexValidatorFactoryMock = $ this ->getMockBuilder (RegexFactory::class)
74
99
->disableOriginalConstructor ()
75
100
->setMethods (['create ' ])
76
101
->getMock ();
77
- $ resultForwardFactory -> expects ( $ this ->any ())
78
- ->method ('create ' )
79
- ->willReturn ($ this ->resultForward );
102
+ $ this -> regexValidatorMock = $ this ->createMock (Regex::class);
103
+ $ this -> regexValidatorFactoryMock ->method ('create ' )
104
+ ->willReturn ($ this ->regexValidatorMock );
80
105
106
+ $ this ->regexValidator = new regexValidator ($ this ->regexValidatorFactoryMock );
81
107
$ this ->action = (new ObjectManager ($ this ))->getObject (
82
108
NewAction::class,
83
109
[
84
110
'context ' => $ this ->initContext (),
85
111
'productBuilder ' => $ this ->productBuilder ,
86
112
'resultPageFactory ' => $ resultPageFactory ,
87
- 'resultForwardFactory ' => $ resultForwardFactory ,
113
+ 'resultForwardFactory ' => $ this ->resultForwardFactory ,
114
+ 'regexValidator ' => $ this ->regexValidator ,
88
115
]
89
116
);
90
117
}
91
118
92
- public function testExecute ()
119
+ /**
120
+ * Test execute method input validation.
121
+ *
122
+ * @param string $value
123
+ * @param bool $exceptionThrown
124
+ * @dataProvider validationCases
125
+ */
126
+ public function testExecute (string $ value , bool $ exceptionThrown ): void
127
+ {
128
+ if ($ exceptionThrown ) {
129
+ $ this ->action ->getRequest ()->expects ($ this ->any ())
130
+ ->method ('getParam ' )
131
+ ->willReturn ($ value );
132
+ $ this ->resultForwardFactory ->expects ($ this ->any ())
133
+ ->method ('create ' )
134
+ ->willReturn ($ this ->resultForward );
135
+ $ this ->resultForward ->expects ($ this ->once ())
136
+ ->method ('forward ' )
137
+ ->with ('noroute ' )
138
+ ->willReturn (true );
139
+ $ this ->assertTrue ($ this ->action ->execute ());
140
+ } else {
141
+ $ this ->action ->getRequest ()->expects ($ this ->any ())->method ('getParam ' )->willReturn ($ value );
142
+ $ this ->regexValidatorMock ->expects ($ this ->any ())
143
+ ->method ('isValid ' )
144
+ ->with ($ value )
145
+ ->willReturn (true );
146
+
147
+ $ this ->assertEquals (true , $ this ->regexValidator ->validateParamRegex ($ value ));
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Validation cases.
153
+ *
154
+ * @return array
155
+ */
156
+ public function validationCases (): array
93
157
{
94
- $ this -> action -> getRequest ()-> expects ( $ this -> any ())-> method ( ' getParam ' )-> willReturn ( true );
95
- $ this -> action -> getRequest ()-> expects ( $ this -> any ())-> method ( ' getFullActionName ' )
96
- -> willReturn ( 'catalog_product_new ' );
97
- $ this -> action -> execute () ;
158
+ return [
159
+ ' execute-with-exception ' => [ ' simple \' and true()]|*[self%3a%3ahandle%20or%20self%3a%3alayout ' , true ],
160
+ ' execute-without-exception ' => [ 'catalog_product_new ' , false ]
161
+ ] ;
98
162
}
99
163
}
0 commit comments