File tree Expand file tree Collapse file tree
tests/TestCase/Controller/Traits Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -43,6 +43,11 @@ public function changePassword()
4343 } else {
4444 $ user ->id = $ this ->request ->session ()->read (Configure::read ('Users.Key.Session.resetPasswordUserId ' ));
4545 $ validatePassword = false ;
46+ if (!$ user ->id ) {
47+ $ this ->Flash ->error (__d ('CakeDC/Users ' , 'User was not found ' ));
48+ $ this ->redirect ($ this ->Auth ->config ('loginAction ' ));
49+ return ;
50+ }
4651 //@todo add to the documentation: list of routes used
4752 $ redirect = $ this ->Auth ->config ('loginAction ' );
4853 }
Original file line number Diff line number Diff line change @@ -87,15 +87,40 @@ public function tearDown()
8787 parent ::tearDown ();
8888 }
8989
90+ /**
91+ * Mock session and mock session attributes
92+ *
93+ * @return void
94+ */
95+ protected function _mockSession ($ attributes )
96+ {
97+ $ session = new \Cake \Network \Session ();
98+
99+ foreach ($ attributes as $ field => $ value ) {
100+ $ session ->write ($ field , $ value );
101+ }
102+
103+ $ this ->Trait ->request
104+ ->expects ($ this ->any ())
105+ ->method ('session ' )
106+ ->willReturn ($ session );
107+ }
108+
90109 /**
91110 * mock request for GET
92111 *
93112 * @return void
94113 */
95- protected function _mockRequestGet ()
114+ protected function _mockRequestGet ($ withSession = false )
96115 {
116+ $ methods = ['is ' , 'referer ' , 'data ' ];
117+
118+ if ($ withSession ) {
119+ $ methods [] = 'session ' ;
120+ }
121+
97122 $ this ->Trait ->request = $ this ->getMockBuilder ('Cake\Network\Request ' )
98- ->setMethods ([ ' is ' , ' referer ' , ' data ' ] )
123+ ->setMethods ($ methods )
99124 ->getMock ();
100125 $ this ->Trait ->request ->expects ($ this ->any ())
101126 ->method ('is ' )
Original file line number Diff line number Diff line change @@ -215,17 +215,37 @@ public function testChangePasswordGetLoggedIn()
215215 *
216216 * @return void
217217 */
218- public function testChangePasswordGetNotLoggedIn ()
218+ public function testChangePasswordGetNotLoggedInInsideResetPasswordFlow ()
219219 {
220- $ this ->_mockRequestGet ();
220+ $ this ->_mockRequestGet (true );
221221 $ this ->_mockAuth ();
222+ $ this ->_mockFlash ();
223+ $ this ->_mockSession ([
224+ Configure::read ('Users.Key.Session.resetPasswordUserId ' ) => '00000000-0000-0000-0000-000000000001 '
225+ ]);
222226 $ this ->Trait ->expects ($ this ->any ())
223- ->method ('set ' )
224- ->will ($ this ->returnCallback (function ($ param1 , $ param2 = null ) {
225- if ($ param1 === 'validatePassword ' ) {
226- TestCase::assertEquals ($ param2 , false );
227- }
228- }));
227+ ->method ('set ' )
228+ ->will ($ this ->returnCallback (function ($ param1 , $ param2 = null ) {
229+ if ($ param1 === 'validatePassword ' ) {
230+ TestCase::assertEquals ($ param2 , false );
231+ }
232+ }));
233+ $ this ->Trait ->changePassword ();
234+ }
235+
236+ /**
237+ * test
238+ *
239+ * @return void
240+ */
241+ public function testChangePasswordGetNotLoggedInOutsideResetPasswordFlow ()
242+ {
243+ $ this ->_mockRequestGet ();
244+ $ this ->_mockAuth ();
245+ $ this ->_mockFlash ();
246+ $ this ->Trait ->Flash ->expects ($ this ->once ())
247+ ->method ('error ' )
248+ ->with ('User was not found ' );
229249 $ this ->Trait ->changePassword ();
230250 }
231251
You can’t perform that action at this time.
0 commit comments