44
55use yii \mongodb \Session ;
66use Yii ;
7+ use yii \helpers \ArrayHelper ;
78
89class SessionTest extends TestCase
910{
@@ -22,13 +23,13 @@ protected function tearDown()
2223 * Creates test session instance.
2324 * @return Session session instance.
2425 */
25- protected function createSession ()
26+ protected function createSession ($ config = [] )
2627 {
27- return Yii::createObject ([
28+ return Yii::createObject (ArrayHelper:: merge ( [
2829 'class ' => Session::className (),
2930 'db ' => $ this ->getConnection (),
3031 'sessionCollection ' => static ::$ sessionCollection ,
31- ]);
32+ ], $ config ) );
3233 }
3334
3435 // Tests:
@@ -157,4 +158,47 @@ public function testWriteCustomField()
157158 $ this ->assertEquals ('session data ' , $ rows [0 ]['data ' ]);
158159 $ this ->assertEquals (15 , $ rows [0 ]['user_id ' ]);
159160 }
160- }
161+
162+ /**
163+ * @depends testWriteSession
164+ * @runInSeparateProcess
165+ */
166+ public function testStrictMode ()
167+ {
168+ //non-strict-mode test
169+ $ nonStrictSession = $ this ->createSession ([
170+ 'useStrictMode ' => false ,
171+ ]);
172+ $ nonStrictSession ->close ();
173+ $ nonStrictSession ->destroySession ('non-existing-non-strict ' );
174+ $ nonStrictSession ->setId ('non-existing-non-strict ' );
175+ $ nonStrictSession ->open ();
176+ $ this ->assertEquals ('non-existing-non-strict ' , $ nonStrictSession ->getId ());
177+ $ nonStrictSession ->close ();
178+
179+ //strict-mode test
180+ $ strictSession = $ this ->createSession ([
181+ 'useStrictMode ' => true ,
182+ ]);
183+ $ strictSession ->close ();
184+ $ strictSession ->destroySession ('non-existing-strict ' );
185+ $ strictSession ->setId ('non-existing-strict ' );
186+ $ strictSession ->open ();
187+ $ id = $ strictSession ->getId ();
188+ $ this ->assertNotEquals ('non-existing-strict ' , $ id );
189+ $ strictSession ->set ('strict_mode_test ' , 'session data ' );
190+ $ strictSession ->close ();
191+ //Ensure session was not stored under forced id
192+ $ strictSession ->setId ('non-existing-strict ' );
193+ $ strictSession ->open ();
194+ $ this ->assertNotEquals ('session data ' , $ strictSession ->get ('strict_mode_test ' ));
195+ $ strictSession ->close ();
196+ //Ensure session can be accessed with the new (and thus existing) id.
197+ $ strictSession ->setId ($ id );
198+ $ strictSession ->open ();
199+ $ this ->assertNotEmpty ($ id );
200+ $ this ->assertEquals ($ id , $ strictSession ->getId ());
201+ $ this ->assertEquals ('session data ' , $ strictSession ->get ('strict_mode_test ' ));
202+ $ strictSession ->close ();
203+ }
204+ }
0 commit comments