@@ -21,8 +21,22 @@ class FeatureContext extends MinkContext
21
21
private $ params = array ();
22
22
private $ data = array ();
23
23
24
+ /**
25
+ * @var mysqli
26
+ */
24
27
private $ db ;
25
28
29
+ /**
30
+ * Null if user is not logged in
31
+ * @var string
32
+ */
33
+ private $ token ;
34
+
35
+ /**
36
+ * @var array
37
+ */
38
+ private $ currentUser ;
39
+
26
40
/**
27
41
* Initializes context.
28
42
* Every scenario gets its own context object.
@@ -157,6 +171,74 @@ public function doSomethingNotThereYet()
157
171
// doSomethingWith($argument);
158
172
// }
159
173
//
174
+ /**
175
+ * @Given /^I have logged in as an administrator$/
176
+ */
177
+ public function iAmAuthenticatedAsAdmin () {
178
+ $ this ->visit ('/lists/admin/ ' );
179
+ $ this ->fillField ('login ' , $ this ->params ['admin_username ' ]);
180
+ $ this ->fillField ('password ' , $ this ->params ['admin_password ' ]);
181
+ $ this ->pressButton ('Continue ' );
182
+
183
+ if (null === $ this ->getSession ()->getPage ()->find ('named ' , array ('content ' , 'Dashboard ' ))) {
184
+ $ this ->throwExpectationException ('Login failed: Dashboard link not found ' );
185
+ }
186
+
187
+ // store current token
188
+ $ link = $ this ->getSession ()->getPage ()->findLink ('dashboard ' );
189
+ $ href = $ link ->getAttribute ('href ' );
190
+ $ this ->token = substr ($ href ,strpos ($ href ,'tk= ' )+3 );
191
+ $ this ->currentUser = $ this ->generateCurrentUserInfo ($ this ->params ['admin_username ' ]);
192
+ }
193
+
194
+ /**
195
+ * @param $name
196
+ * @return array
197
+ * @throws Exception
198
+ */
199
+ private function generateCurrentUserInfo ($ name )
200
+ {
201
+ $ db = $ this ->getMysqli ();
202
+ $ query = sprintf (
203
+ 'SELECT * from %s where loginname="%s" ' ,
204
+ 'phplist_admin ' ,
205
+ $ name
206
+ );
207
+ $ results = $ db ->query ($ query )->fetch_assoc ();
208
+ if (!isset ($ results ['id ' ]) ){
209
+ throw new \Exception ($ db ->error );
210
+ }
211
+ return $ results ;
212
+ }
213
+ /**
214
+ * @return bool
215
+ */
216
+ public function isLoggedIn ($ throwsException = false )
217
+ {
218
+ $ retVal = $ this ->token != null ;
219
+ if (!$ retVal && $ throwsException ){
220
+ throw new \Exception ('Not logged in yet ' );
221
+ }
222
+ return $ retVal ;
223
+ }
224
+
225
+ /**
226
+ * @return array
227
+ */
228
+ public function getCurrentUser ()
229
+ {
230
+ $ this ->isLoggedIn (true );
231
+ return $ this ->currentUser ;
232
+ }
233
+
234
+ /**
235
+ * @return string
236
+ */
237
+ public function getToken ()
238
+ {
239
+ $ this ->isLoggedIn (true );
240
+ return $ this ->token ;
241
+ }
160
242
161
243
/**
162
244
* @When /^I recreate the database$/
@@ -223,16 +305,54 @@ public function iHaveNotYetCreatedCampaigns()
223
305
}
224
306
225
307
/**
226
- * @Given /^I have logged in as an administrator$ /
308
+ * @Given /^I have subscriber with email "([^"]*)" /
227
309
*/
228
- public function iAmAuthenticatedAsAdmin () {
229
- $ this ->visit ('/lists/admin/ ' );
230
- $ this ->fillField ('login ' , $ this ->params ['admin_username ' ]);
231
- $ this ->fillField ('password ' , $ this ->params ['admin_password ' ]);
232
- $ this ->pressButton ('Continue ' );
233
-
234
- if (null === $ this ->getSession ()->getPage ()->find ('named ' , array ('content ' , 'Dashboard ' ))) {
235
- $ this ->throwExpectationException ('Login failed: Dashboard link not found ' );
310
+ public function iHaveSubscriber ($ email )
311
+ {
312
+ $ this ->clickLink ('S ' );
313
+ }
314
+
315
+ /**
316
+ * @return mysqli
317
+ */
318
+ public function getMysqli ()
319
+ {
320
+ return $ this ->db ;
321
+ }
322
+
323
+ /**
324
+ * @var array $params
325
+ * @return string
326
+ */
327
+ public function generateUrl ($ params )
328
+ {
329
+ $ token = $ this ->getToken ();
330
+ $ params ['tk ' ] = $ token ;
331
+ $ url = $ this ->getSession ()->getCurrentUrl ();
332
+
333
+ $ queryPath = [];
334
+ foreach ($ params as $ name =>$ value ){
335
+ $ queryPath [] = $ name .'= ' .$ value ;
236
336
}
337
+ $ link = $ url .'? ' .implode ('& ' ,$ queryPath );
338
+ return $ link ;
339
+ }
340
+
341
+ /**
342
+ * @param $num
343
+ * @Then /^I wait for .* (second|seconds)/
344
+ */
345
+ public function iWaitForSeconds ($ num )
346
+ {
347
+ $ num = (int ) $ num ;
348
+ sleep ($ num );
349
+ }
350
+
351
+ /**
352
+ * @Then /^I wait for the ajax response$/
353
+ */
354
+ public function iWaitForTheAjaxResponse ()
355
+ {
356
+ $ this ->getSession ()->wait (5000 , '(0 === jQuery.active) ' );
237
357
}
238
358
}
0 commit comments