FeatureContext.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. use Behat\MinkExtension\Context\MinkContext;
  3. /**
  4. * Features context. (MinkContext extends BehatContext)
  5. */
  6. class FeatureContext extends MinkContext
  7. {
  8. /**
  9. * Initializes context.
  10. * Every scenario gets its own context object.
  11. */
  12. public function __construct()
  13. {
  14. }
  15. /**
  16. * @Given /^I am a platform administrator$/
  17. */
  18. public function iAmAPlatformAdministrator()
  19. {
  20. $this->visit('/index.php?logout=logout');
  21. $this->iAmOnHomepage();
  22. $this->fillField('login', 'admin');
  23. $this->fillField('password', 'admin');
  24. $this->pressButton('submitAuth');
  25. $this->getSession()->back();
  26. }
  27. /**
  28. * @Given /^I am a session administrator$/
  29. */
  30. public function iAmASessionAdministrator()
  31. {
  32. $this->visit('/index.php?logout=logout');
  33. $this->iAmOnHomepage();
  34. $this->fillFields(new \Behat\Gherkin\Node\TableNode([
  35. ['login', 'amaurichard'],
  36. ['password', 'amaurichard']
  37. ]));
  38. $this->pressButton('submitAuth');
  39. }
  40. /**
  41. * @Given /^I am a teacher$/
  42. */
  43. public function iAmATeacher()
  44. {
  45. $this->visit('/index.php?logout=logout');
  46. $this->iAmOnHomepage();
  47. $this->fillField('login', 'mmosquera');
  48. $this->fillField('password', 'mmosquera');
  49. $this->pressButton('submitAuth');
  50. }
  51. /**
  52. * @Given /^I am a teacher in course "([^"]*)"$/
  53. * @Todo implement
  54. */
  55. public function iAmATeacherInCourse($course)
  56. {
  57. //$sql = "SELECT * FROM course_rel_user WHERE c_id = X AND user_id = ";
  58. //$result = ...
  59. //if ($result !== false) { ... }
  60. }
  61. /**
  62. * @Given /^I am a student$/
  63. */
  64. public function iAmAStudent()
  65. {
  66. $this->visit('/index.php?logout=logout');
  67. $this->iAmOnHomepage();
  68. $this->fillField('login', 'acostea');
  69. $this->fillField('password', 'acostea');
  70. $this->pressButton('submitAuth');
  71. }
  72. /**
  73. * @Given /^I am an HR manager$/
  74. */
  75. public function iAmAnHR()
  76. {
  77. $this->visit('/index.php?logout=logout');
  78. $this->iAmOnHomepage();
  79. $this->fillField('login', 'ptook');
  80. $this->fillField('password', 'ptook');
  81. $this->pressButton('submitAuth');
  82. }
  83. /**
  84. * @Given /^I am a student boss$/
  85. */
  86. public function iAmAStudentBoss()
  87. {
  88. $this->visit('/index.php?logout=logout');
  89. $this->iAmOnHomepage();
  90. $this->fillField('login', 'abaggins');
  91. $this->fillField('password', 'abaggins');
  92. $this->pressButton('submitAuth');
  93. }
  94. /**
  95. * @Given /^I am an invitee$/
  96. */
  97. public function iAmAnInvitee()
  98. {
  99. $this->visit('/index.php?logout=logout');
  100. $this->iAmOnHomepage();
  101. $this->fillField('login', 'bproudfoot');
  102. $this->fillField('password', 'bproudfoot');
  103. $this->pressButton('submitAuth');
  104. }
  105. /**
  106. * @Given /^course "([^"]*)" exists$/
  107. */
  108. public function courseExists($argument)
  109. {
  110. $this->iAmAPlatformAdministrator();
  111. $this->visit('/main/admin/course_list.php?keyword=' . $argument);
  112. $this->assertPageContainsText($argument);
  113. }
  114. /**
  115. * @Given /^course "([^"]*)" is deleted$/
  116. */
  117. public function courseIsDeleted($argument)
  118. {
  119. $this->iAmAPlatformAdministrator();
  120. $this->visit('/main/admin/course_list.php?keyword=' . $argument);
  121. $this->clickLink('Delete');
  122. }
  123. /**
  124. * @Given /^I am in course "([^"]*)"$/
  125. * @Todo redefine function to be different from I am on course TEMP homepage
  126. */
  127. public function iAmInCourse($argument)
  128. {
  129. $this->visit('/main/course_home/course_home.php?cDir=' . $argument);
  130. $this->assertElementNotOnPage('.alert-danger');
  131. }
  132. /**
  133. * @Given /^I am on course "([^"]*)" homepage$/
  134. */
  135. public function iAmOnCourseXHomepage($argument)
  136. {
  137. $this->visit('/main/course_home/course_home.php?cDir=' . $argument);
  138. $this->assertElementNotOnPage('.alert-danger');
  139. }
  140. /**
  141. * @Given /^I am a "([^"]*)" user$/
  142. */
  143. public function iAmAXUser($argument)
  144. {
  145. $this->visit('/main/auth/profile.php');
  146. $this->assertFieldContains('language', $argument);
  147. }
  148. /**
  149. * @Given /^I am logged as "([^"]*)"$/
  150. */
  151. public function iAmLoggedAs($username)
  152. {
  153. $this->visit('/index.php?logout=logout');
  154. $this->iAmOnHomepage();
  155. $this->fillFields(new \Behat\Gherkin\Node\TableNode([
  156. ['login', $username],
  157. ['password', $username]
  158. ]));
  159. $this->pressButton('submitAuth');
  160. }
  161. /**
  162. * @Given /^I have a friend named "([^"]*)" with id "([^"]*)"$/
  163. */
  164. public function iHaveAFriend($friendUsername, $friendId)
  165. {
  166. $adminId = 1;
  167. $friendId = $friendId;
  168. $friendUsername = $friendUsername;
  169. $sendInvitationURL = '/main/inc/ajax/message.ajax.php?' . http_build_query([
  170. 'a' => 'send_invitation',
  171. 'user_id' => $friendId,
  172. 'content' => 'Add me'
  173. ]);
  174. $acceptInvitationURL = '/main/inc/ajax/social.ajax.php?' . http_build_query([
  175. 'a' => 'add_friend',
  176. 'friend_id' => $adminId,
  177. 'is_my_friend' => 'friend'
  178. ]);
  179. $this->iAmAPlatformAdministrator();
  180. $this->visit($sendInvitationURL);
  181. $this->iAmLoggedAs($friendUsername);
  182. $this->visit($acceptInvitationURL);
  183. $this->iAmAPlatformAdministrator();
  184. }
  185. /**
  186. * @Given /^I have a public password-protected course named "([^"]*)" with password "([^"]*)"$/
  187. */
  188. public function iHaveAPublicPasswordProtectedCourse($code, $password)
  189. {
  190. $this->visit('/main/admin/course_add.php');
  191. $this->fillFields(new \Behat\Gherkin\Node\TableNode([
  192. ['title', 'Password Protected'],
  193. ['visual_code', $code],
  194. ['visibility', 3]
  195. ]));
  196. $this->pressButton('submit');
  197. $this->visit('/main/course_info/infocours.php?cidReq=' . $code);
  198. $this->assertPageContainsText('Course registration password');
  199. $this->fillField('course_registration_password', $password);
  200. $this->pressButton('submit_save');
  201. $this->assertFieldContains('course_registration_password', $password);
  202. }
  203. /**
  204. * @Given /^I am not logged$/
  205. */
  206. public function iAmNotLogged()
  207. {
  208. $this->visit('/index.php?logout=logout');
  209. $this->visit('I am on homepage');
  210. }
  211. /**
  212. * @When /^I invite to a friend with id "([^"]*)" to a social group with id "([^"]*)"$/
  213. */
  214. public function iInviteAFrienToASocialGroup($friendId, $groupId)
  215. {
  216. $this->visit('/main/social/group_invitation.php?id=' . $groupId);
  217. $this->fillField('invitation[]', $friendId);
  218. $this->pressButton('submit');
  219. }
  220. /**
  221. * Sometimes the top admin toolbar has form buttons
  222. * that conflicts with the main page forms so we need
  223. * to disable it
  224. * @Given /^Admin top bar is disabled$/
  225. */
  226. public function adminTopBarIsDisabled()
  227. {
  228. $this->iAmAPlatformAdministrator();
  229. $this->visit('/main/admin/settings.php');
  230. $this->fillField('search_field', 'show_admin_toolbar');
  231. $this->pressButton('submit_button');
  232. $this->selectOption('show_admin_toolbar', 'do_not_show');
  233. $this->pressButton('submit');
  234. }
  235. /**
  236. * @Given /^Admin top bar is enabled$/
  237. */
  238. public function adminTopBarIsEnabled()
  239. {
  240. $this->iAmAPlatformAdministrator();
  241. $this->visit('/main/admin/settings.php');
  242. $this->fillField('search_field', 'show_admin_toolbar');
  243. $this->pressButton('submit_button');
  244. $this->selectOption('show_admin_toolbar', 'show_to_admin_and_teachers');
  245. $this->pressButton('submit');
  246. }
  247. /**
  248. * @Given /^I am on the social group members page with id "([^"]*)"$/
  249. */
  250. public function iAmOnSocialGroupMembersPageWithId($groupId)
  251. {
  252. $this->visit('/main/social/group_view.php?id=' . $groupId);
  253. }
  254. /**
  255. * @When /^I try delete a friend with id "([^"]*)" from the social group with id "([^"]*)"$/
  256. */
  257. public function iTryDeleteAFriendFromSocialGroup($friendId, $groupId)
  258. {
  259. $this->visit('/main/social/group_members.php?' . http_build_query([
  260. 'id' => $groupId,
  261. 'u' => $friendId,
  262. 'action' => 'delete'
  263. ]));
  264. }
  265. /**
  266. * @Then /^I fill in ckeditor field "([^"]*)" with "([^"]*)"$/
  267. */
  268. public function iFillInWysiwygOnFieldWith($locator, $value)
  269. {
  270. // Just in case wait that ckeditor is loaded
  271. $this->getSession()->wait(2000);
  272. $el = $this->getSession()->getPage()->findField($locator);
  273. $fieldId = $el->getAttribute('id');
  274. if (empty($fieldId)) {
  275. throw new Exception(
  276. 'Could not find an id for field with locator: '.$locator
  277. );
  278. }
  279. $this->getSession()->executeScript(
  280. "CKEDITOR.instances[\"$fieldId\"].setData(\"$value\");"
  281. );
  282. }
  283. /**
  284. * @Given /^I fill hidden field "([^"]*)" with "([^"]*)"$/
  285. */
  286. public function iFillHiddenFieldWith($field, $value)
  287. {
  288. $this->getSession()->getPage()->find(
  289. 'css',
  290. 'input[name="'.$field.'"]'
  291. )->setValue($value);
  292. }
  293. /**
  294. * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with id "(?P<id>(?:[^"]|\\")*)" and value "(?P<value>(?:[^"]|\\")*)"$/
  295. */
  296. public function iFillInSelectInputWithAndSelect($field, $id, $value)
  297. {
  298. $this->getSession()->executeScript("$('$field').select2({data : [{id: $id, text: '$value'}]});");
  299. }
  300. /**
  301. * @When /^(?:|I )confirm the popup$/
  302. */
  303. public function confirmPopup()
  304. {
  305. // See
  306. // https://gist.github.com/blazarecki/2888851
  307. /** @var \Behat\Mink\Driver\Selenium2Driver $driver Needed because no cross-driver way yet */
  308. $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
  309. }
  310. /**
  311. * @When /^(?:|I )fill in select bootstrap input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/
  312. */
  313. public function iFillInSelectBootstrapInputWithAndSelect($field, $value, $entry)
  314. {
  315. $page = $this->getSession()->getPage();
  316. $inputField = $page->find('css', $field);
  317. if (!$inputField) {
  318. throw new \Exception('No field found');
  319. }
  320. $choice = $inputField->getParent()->find('css', '.bootstrap-select');
  321. if (!$choice) {
  322. throw new \Exception('No select bootstrap choice found');
  323. }
  324. $choice->press();
  325. $selectInput = $inputField->getParent()->find('css', '.bootstrap-select .form-control');
  326. if (!$selectInput) {
  327. throw new \Exception('No input found');
  328. }
  329. $selectInput->setValue($value);
  330. $this->getSession()->wait(3000);
  331. $chosenResults = $inputField->getParent()->findAll('css', '.dropdown-menu inner li');
  332. foreach ($chosenResults as $result) {
  333. //$option = $result->find('css', '.text');
  334. if ($result->getText() == $entry) {
  335. $result->click();
  336. break;
  337. }
  338. }
  339. }
  340. /**
  341. * @When /^(?:|I )fill in select bootstrap static input "(?P<field>(?:[^"]|\\")*)" select "(?P<value>(?:[^"]|\\")*)"$/
  342. */
  343. public function iFillInSelectStaticBootstrapInputWithAndSelect($field, $value)
  344. {
  345. $this->getSession()->wait(1000);
  346. $this->getSession()->executeScript("
  347. $(function() {
  348. $('$field').selectpicker('val', '$value');
  349. });
  350. ");
  351. }
  352. /**
  353. * @When /^wait for the page to be loaded$/
  354. */
  355. public function waitForThePageToBeLoaded()
  356. {
  357. //$this->getSession()->wait(10000, "document.readyState === 'complete'");
  358. $this->getSession()->wait(3000);
  359. }
  360. /**
  361. * @When /^I check the "([^"]*)" radio button$/
  362. */
  363. public function iCheckTheRadioButton($radioLabel)
  364. {
  365. $radioButton = $this->getSession()->getPage()->findField($radioLabel);
  366. if (null === $radioButton) {
  367. throw new Exception("Cannot find radio button ".$radioLabel);
  368. }
  369. //$value = $radioButton->getAttribute('value');
  370. $this->getSession()->getDriver()->click($radioButton->getXPath());
  371. }
  372. /**
  373. * @When /^I check radio button with label "([^"]*)"$/
  374. */
  375. public function iCheckTheRadioButtonWithLabel($label)
  376. {
  377. $this->getSession()->executeScript("
  378. $(function() {
  379. $(':contains(\$label\")').parent().find('input').prop('checked', true);
  380. });
  381. ");
  382. }
  383. /**
  384. * @When /^I press advanced settings$/
  385. */
  386. public function iSelectFromSelectWithLabel()
  387. {
  388. $this->pressButton('Advanced settings');
  389. }
  390. /**
  391. * Clicks link with specified id|title|alt|text
  392. * Example: When I follow "Log In"
  393. * Example: And I follow "Log In"
  394. *
  395. * @When /^(?:|I )focus "(?P<link>(?:[^"]|\\")*)"$/
  396. */
  397. public function focus($input)
  398. {
  399. $input = $this->getSession()->getPage()->findField($input);
  400. $input->focus();
  401. }
  402. /**
  403. * @Given /^I check the "([^"]*)" radio button with "([^"]*)" value$/
  404. */
  405. public function iCheckTheRadioButtonWithValue($element, $value)
  406. {
  407. $this->getSession()->executeScript("
  408. $(function() {
  409. $('input[type=\"radio\"][name=".$element."][value=".$value."]').prop('checked', true);
  410. });
  411. ");
  412. return true;
  413. }
  414. }