FeatureContext.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  1. <?php
  2. use Behat\Gherkin\Node\TableNode;
  3. use Behat\MinkExtension\Context\MinkContext;
  4. /**
  5. * Features context. (MinkContext extends BehatContext)
  6. */
  7. class FeatureContext extends MinkContext
  8. {
  9. /**
  10. * Initializes context.
  11. * Every scenario gets its own context object.
  12. */
  13. public function __construct()
  14. {
  15. }
  16. /**
  17. * @Given /^I am a platform administrator$/
  18. */
  19. public function iAmAPlatformAdministrator()
  20. {
  21. $this->visit('/index.php?logout=logout&uid=1');
  22. $this->iAmOnHomepage();
  23. $this->fillField('login', 'admin');
  24. $this->fillField('password', 'admin');
  25. $this->pressButton('submitAuth');
  26. $this->getSession()->back();
  27. }
  28. /**
  29. * @Given /^I am a teacher$/
  30. */
  31. public function iAmATeacher()
  32. {
  33. $this->visit('/index.php?logout=logout');
  34. $this->iAmOnHomepage();
  35. $this->fillField('login', 'mmosquera');
  36. $this->fillField('password', 'mmosquera');
  37. $this->pressButton('submitAuth');
  38. }
  39. /**
  40. * @Given /^I am a student$/
  41. */
  42. public function iAmAStudent()
  43. {
  44. $this->visit('/index.php?logout=logout');
  45. $this->iAmOnHomepage();
  46. $this->fillField('login', 'acostea');
  47. $this->fillField('password', 'acostea');
  48. $this->pressButton('submitAuth');
  49. }
  50. /**
  51. * @Given /^I am an HR manager$/
  52. */
  53. public function iAmAnHR()
  54. {
  55. $this->visit('/index.php?logout=logout');
  56. $this->iAmOnHomepage();
  57. $this->fillField('login', 'ptook');
  58. $this->fillField('password', 'ptook');
  59. $this->pressButton('submitAuth');
  60. }
  61. /**
  62. * @Given /^I am a student boss$/
  63. */
  64. public function iAmAStudentBoss()
  65. {
  66. $this->visit('/index.php?logout=logout');
  67. $this->iAmOnHomepage();
  68. $this->fillField('login', 'abaggins');
  69. $this->fillField('password', 'abaggins');
  70. $this->pressButton('submitAuth');
  71. }
  72. /**
  73. * @Given /^I am an invitee$/
  74. */
  75. public function iAmAnInvitee()
  76. {
  77. $this->visit('/index.php?logout=logout');
  78. $this->iAmOnHomepage();
  79. $this->fillField('login', 'bproudfoot');
  80. $this->fillField('password', 'bproudfoot');
  81. $this->pressButton('submitAuth');
  82. }
  83. /**
  84. * @Given /^course "([^"]*)" exists$/
  85. */
  86. public function courseExists($argument)
  87. {
  88. $this->iAmAPlatformAdministrator();
  89. $this->visit('/main/admin/course_list.php?keyword='.$argument);
  90. $this->assertPageContainsText($argument);
  91. }
  92. /**
  93. * @Given /^course "([^"]*)" is deleted$/
  94. */
  95. public function courseIsDeleted($argument)
  96. {
  97. $this->iAmAPlatformAdministrator();
  98. $this->visit('/main/admin/course_list.php?keyword='.$argument);
  99. $this->clickLink('Delete');
  100. }
  101. /**
  102. * @Given /^I am on course "([^"]*)" homepage$/
  103. */
  104. public function iAmOnCourseXHomepage($courseCode)
  105. {
  106. $this->visit('/courses/'.$courseCode.'/index.php');
  107. $this->assertElementNotOnPage('.alert-danger');
  108. }
  109. /**
  110. * @Given /^I am on course "([^"]*)" homepage in session "([^"]*)"$/
  111. */
  112. public function iAmOnCourseXHomepageInSessionY($courseCode, $sessionName)
  113. {
  114. $this->visit('/main/course_home/redirect.php?cidReq='.$courseCode.'&session_name='.$sessionName);
  115. }
  116. /**
  117. * @Given /^I am a "([^"]*)" user$/
  118. */
  119. public function iAmAXUser($argument)
  120. {
  121. $this->visit('/main/auth/profile.php');
  122. $this->assertFieldContains('language', $argument);
  123. }
  124. /**
  125. * @Given /^I am logged as "([^"]*)"$/
  126. */
  127. public function iAmLoggedAs($username)
  128. {
  129. $this->visit('/index.php?logout=logout');
  130. $this->iAmOnHomepage();
  131. $this->fillFields(
  132. new TableNode(
  133. [
  134. ['login', $username],
  135. ['password', $username],
  136. ]
  137. )
  138. );
  139. $this->pressButton('submitAuth');
  140. }
  141. /**
  142. * @Given /^I have a friend named "([^"]*)" with id "([^"]*)"$/
  143. */
  144. public function iHaveAFriend($friendUsername, $friendId)
  145. {
  146. $adminId = 1;
  147. $friendId = $friendId;
  148. $friendUsername = $friendUsername;
  149. $sendInvitationURL = '/main/inc/ajax/message.ajax.php?'.
  150. http_build_query(
  151. [
  152. 'a' => 'send_invitation',
  153. 'user_id' => $friendId,
  154. 'content' => 'Add me',
  155. ]
  156. );
  157. $acceptInvitationURL = '/main/inc/ajax/social.ajax.php?'.
  158. http_build_query(
  159. [
  160. 'a' => 'add_friend',
  161. 'friend_id' => $adminId,
  162. 'is_my_friend' => 'friend',
  163. ]
  164. );
  165. $this->iAmAPlatformAdministrator();
  166. $this->visit($sendInvitationURL);
  167. $this->iAmLoggedAs($friendUsername);
  168. $this->visit($acceptInvitationURL);
  169. $this->iAmAPlatformAdministrator();
  170. }
  171. /**
  172. * @Given /^I have a public password-protected course named "([^"]*)" with password "([^"]*)"$/
  173. */
  174. public function iHaveAPublicPasswordProtectedCourse($code, $password)
  175. {
  176. $this->visit('/main/admin/course_add.php');
  177. $this->fillFields(
  178. new TableNode(
  179. [
  180. ['title', 'Password Protected'],
  181. ['visual_code', $code],
  182. ['visibility', 3],
  183. ]
  184. )
  185. );
  186. $this->pressButton('submit');
  187. $this->visit('/main/course_info/infocours.php?cidReq='.$code);
  188. $this->assertPageContainsText('Course registration password');
  189. $this->fillField('course_registration_password', $password);
  190. $this->pressButton('submit_save');
  191. $this->assertFieldContains('course_registration_password', $password);
  192. }
  193. /**
  194. * @Given /^I am not logged$/
  195. */
  196. public function iAmNotLogged()
  197. {
  198. $this->visit('/index.php?logout=logout');
  199. $this->visit('I am on homepage');
  200. }
  201. /**
  202. * @When /^I invite to a friend with id "([^"]*)" to a social group with id "([^"]*)"$/
  203. */
  204. public function iInviteAFriendToASocialGroup($friendId, $groupId)
  205. {
  206. $this->visit('/main/social/group_invitation.php?id='.$groupId);
  207. $this->fillField('invitation[]', $friendId);
  208. $this->pressButton('submit');
  209. }
  210. /**
  211. * Sometimes the top admin toolbar has form buttons
  212. * that conflicts with the main page forms so we need
  213. * to disable it
  214. * @Given /^Admin top bar is disabled$/
  215. */
  216. public function adminTopBarIsDisabled()
  217. {
  218. $this->iAmAPlatformAdministrator();
  219. $this->visit('/main/admin/settings.php');
  220. $this->fillField('search_field', 'show_admin_toolbar');
  221. $this->pressButton('submit_button');
  222. $this->selectOption('show_admin_toolbar', 'do_not_show');
  223. $this->pressButton('submit');
  224. }
  225. /**
  226. * @Given /^Admin top bar is enabled$/
  227. */
  228. public function adminTopBarIsEnabled()
  229. {
  230. $this->iAmAPlatformAdministrator();
  231. $this->visit('/main/admin/settings.php');
  232. $this->fillField('search_field', 'show_admin_toolbar');
  233. $this->pressButton('submit_button');
  234. $this->selectOption('show_admin_toolbar', 'show_to_admin_and_teachers');
  235. $this->pressButton('submit');
  236. }
  237. /**
  238. * @Given /^I am on the social group members page with id "([^"]*)"$/
  239. */
  240. public function iAmOnSocialGroupMembersPageWithId($groupId)
  241. {
  242. $this->visit('/main/social/group_view.php?id='.$groupId);
  243. }
  244. /**
  245. * @When /^I try delete a friend with id "([^"]*)" from the social group with id "([^"]*)"$/
  246. */
  247. public function iTryDeleteAFriendFromSocialGroup($friendId, $groupId)
  248. {
  249. $this->visit(
  250. '/main/social/group_members.php?'.http_build_query(
  251. [
  252. 'id' => $groupId,
  253. 'u' => $friendId,
  254. 'action' => 'delete',
  255. ]
  256. )
  257. );
  258. }
  259. /**
  260. * @Then /^I fill in ckeditor field "([^"]*)" with "([^"]*)"$/
  261. */
  262. public function iFillInWysiwygOnFieldWith($locator, $value)
  263. {
  264. // Just in case wait that ckeditor is loaded
  265. $this->getSession()->wait(2000);
  266. $el = $this->getSession()->getPage()->findField($locator);
  267. $fieldId = $el->getAttribute('id');
  268. if (empty($fieldId)) {
  269. throw new Exception(
  270. 'Could not find an id for field with locator: '.$locator
  271. );
  272. }
  273. $this->getSession()->executeScript(
  274. "CKEDITOR.instances[\"$fieldId\"].setData(\"$value\");"
  275. );
  276. }
  277. /**
  278. * @Then /^I fill the only ckeditor in the page with "([^"]*)"$/
  279. */
  280. public function iFillTheOnlyEditorInThePage($value)
  281. {
  282. // Just in case wait that ckeditor is loaded
  283. $this->getSession()->wait(2000);
  284. $this->getSession()->executeScript(
  285. "
  286. var textarea = $('textarea');
  287. var id = textarea.attr('id');
  288. CKEDITOR.instances[id].setData(\"$value\");
  289. "
  290. );
  291. }
  292. /**
  293. * @Given /^I fill hidden field "([^"]*)" with "([^"]*)"$/
  294. */
  295. public function iFillHiddenFieldWith($field, $value)
  296. {
  297. $this->getSession()->getPage()->find(
  298. 'css',
  299. 'input[name="'.$field.'"]'
  300. )->setValue($value);
  301. }
  302. /**
  303. * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with id "(?P<id>(?:[^"]|\\")*)" and value "(?P<value>(?:[^"]|\\")*)"$/
  304. */
  305. public function iFillInSelectInputWithAndSelect($field, $id, $value)
  306. {
  307. $this->getSession()->executeScript("$('$field').select2({data : [{id: $id, text: '$value'}]});");
  308. }
  309. /**
  310. * @When /^(?:|I )confirm the popup$/
  311. */
  312. public function confirmPopup()
  313. {
  314. // See
  315. // https://gist.github.com/blazarecki/2888851
  316. /** @var \Behat\Mink\Driver\Selenium2Driver $driver Needed because no cross-driver way yet */
  317. $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
  318. }
  319. /**
  320. * @When /^(?:|I )fill in select bootstrap input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/
  321. */
  322. public function iFillInSelectBootstrapInputWithAndSelect($field, $value, $entry)
  323. {
  324. $page = $this->getSession()->getPage();
  325. $inputField = $page->find('css', $field);
  326. if (!$inputField) {
  327. throw new \Exception('No field found');
  328. }
  329. $choice = $inputField->getParent()->find('css', '.bootstrap-select');
  330. if (!$choice) {
  331. throw new \Exception('No select bootstrap choice found');
  332. }
  333. $choice->press();
  334. $selectInput = $inputField->getParent()->find('css', '.bootstrap-select .form-control');
  335. if (!$selectInput) {
  336. throw new \Exception('No input found');
  337. }
  338. $selectInput->setValue($value);
  339. $this->getSession()->wait(3000);
  340. $chosenResults = $inputField->getParent()->findAll('css', '.dropdown-menu inner li');
  341. foreach ($chosenResults as $result) {
  342. //$option = $result->find('css', '.text');
  343. if ($result->getText() == $entry) {
  344. $result->click();
  345. break;
  346. }
  347. }
  348. }
  349. /**
  350. * @When /^(?:|I )fill in select bootstrap static input "(?P<field>(?:[^"]|\\")*)" select "(?P<value>(?:[^"]|\\")*)"$/
  351. */
  352. public function iFillInSelectStaticBootstrapInputWithAndSelect($field, $value)
  353. {
  354. $this->getSession()->wait(1000);
  355. $this->getSession()->executeScript("
  356. $(function() {
  357. $('$field').selectpicker('val', '$value');
  358. });
  359. ");
  360. }
  361. /**
  362. * @When /^(?:|I )fill in select bootstrap static by text "(?P<field>(?:[^"]|\\")*)" select "(?P<value>(?:[^"]|\\")*)"$/
  363. */
  364. public function iFillInSelectStaticBootstrapInputWithAndSelectByText($field, $value)
  365. {
  366. $this->getSession()->wait(1000);
  367. $this->getSession()->executeScript("
  368. $('$field > option').each(function(index, option) {
  369. if (option.text == '$value') {
  370. $('$field').selectpicker('val', option.value);
  371. }
  372. });
  373. ");
  374. }
  375. /**
  376. * @When /^(?:|I )fill in select "(?P<field>(?:[^"]|\\")*)" with option value "(?P<value>(?:[^"]|\\")*)" with class "(?P<id>(?:[^"]|\\")*)"$/
  377. */
  378. public function iFillInSelectWithOptionValue($field, $value, $class)
  379. {
  380. $this->getSession()->wait(1000);
  381. $this->getSession()->executeScript("
  382. var input = $('$field').filter('$class');
  383. var id = input.attr('id');
  384. var input = $('#'+id);
  385. input.val($value);
  386. ");
  387. }
  388. /**
  389. * @When /^wait for the page to be loaded$/
  390. */
  391. public function waitForThePageToBeLoaded()
  392. {
  393. $this->getSession()->wait(3000);
  394. }
  395. /**
  396. * @When /^wait very long for the page to be loaded$/
  397. */
  398. public function waitVeryLongForThePageToBeLoaded()
  399. {
  400. //$this->getSession()->wait(10000, "document.readyState === 'complete'");
  401. $this->getSession()->wait(8000);
  402. }
  403. /**
  404. * @When /^I check the "([^"]*)" radio button$/
  405. */
  406. public function iCheckTheRadioButton($radioLabel)
  407. {
  408. $radioButton = $this->getSession()->getPage()->findField($radioLabel);
  409. if (null === $radioButton) {
  410. throw new Exception("Cannot find radio button ".$radioLabel);
  411. }
  412. //$value = $radioButton->getAttribute('value');
  413. $this->getSession()->getDriver()->click($radioButton->getXPath());
  414. }
  415. /**
  416. * @When /^I check radio button with label "([^"]*)"$/
  417. */
  418. public function iCheckTheRadioButtonWithLabel($label)
  419. {
  420. $this->getSession()->executeScript("
  421. $(function() {
  422. $(':contains(\$label\")').parent().find('input').prop('checked', true);
  423. });
  424. ");
  425. }
  426. /**
  427. * @When /^I press advanced settings$/
  428. */
  429. public function iSelectFromSelectWithLabel()
  430. {
  431. $this->pressButton('Advanced settings');
  432. }
  433. /**
  434. * Clicks link with specified id|title|alt|text
  435. * Example: When I follow "Log In"
  436. * Example: And I follow "Log In"
  437. *
  438. * @When /^(?:|I )focus "(?P<link>(?:[^"]|\\")*)"$/
  439. */
  440. public function focus($input)
  441. {
  442. $input = $this->getSession()->getPage()->findField($input);
  443. $input->focus();
  444. }
  445. /**
  446. * @Given /^I check the "([^"]*)" radio button with "([^"]*)" value$/
  447. */
  448. public function iCheckTheRadioButtonWithValue($element, $value)
  449. {
  450. $this->getSession()->executeScript("
  451. $(function() {
  452. $('input[type=\"radio\"][name=".$element."][value=".$value."]').prop('checked', true);
  453. });
  454. ");
  455. return true;
  456. }
  457. /**
  458. * @Then /^I should see an icon with title "([^"]*)"$/
  459. */
  460. public function iShouldSeeAnIconWithTitle($value)
  461. {
  462. $el = $this->getSession()->getPage()->find('xpath', "//img[@title='$value']");
  463. if (null === $el) {
  464. throw new Exception(
  465. 'Could not find an icon with title: '.$value
  466. );
  467. }
  468. return true;
  469. }
  470. /**
  471. * @Then /^I should not see an icon with title "([^"]*)"$/
  472. */
  473. public function iShouldNotSeeAnIconWithTitle($value)
  474. {
  475. $el = $this->getSession()->getPage()->find('xpath', "//img[@title='$value']");
  476. if (null === $el) {
  477. return true;
  478. }
  479. return false;
  480. }
  481. /**
  482. * @Then /^I save current URL with name "([^"]*)"$/
  483. */
  484. public function saveUrlWithName($name)
  485. {
  486. $url = $this->getSession()->getCurrentUrl();
  487. $this->getSession()->setCookie($name, $url);
  488. }
  489. /**
  490. * @Then /^I visit URL saved with name "([^"]*)"$/
  491. */
  492. public function visitSavedUrlWithName($name)
  493. {
  494. $url = $this->getSession()->getCookie($name);
  495. echo $url;
  496. if (empty($url)) {
  497. throw new Exception("Url with name: $name not found");
  498. }
  499. $this->visit($url);
  500. }
  501. /**
  502. * Example: Then I should see the table "#category_results":
  503. * | Categories | Absolute score | Relative score |
  504. * | Categoryname2 | 50 / 70 | 71.43% |
  505. * | Categoryname1 | 60 / 60 | 100% |
  506. *
  507. * @Then /^I should see the table "([^"]*)":$/
  508. *
  509. * @param string $tableId
  510. * @param TableNode $tableData
  511. *
  512. * @throws Exception
  513. */
  514. public function assertPageContainsTable($tableId, TableNode $tableData)
  515. {
  516. $table = $this->getSession()->getPage()->find('css', $tableId);
  517. $rows = $tableData->getRowsHash();
  518. $i = 1;
  519. $right = array_keys($rows);
  520. foreach ($right as $text) {
  521. $cell = $table->find('css', 'tr:nth-child('.$i.') :nth-child(1)');
  522. $i++;
  523. if (!$cell) {
  524. throw new Exception('Cell not found.');
  525. }
  526. if ($cell->getText() != $text) {
  527. throw new Exception('Table text not found.');
  528. }
  529. }
  530. $i = 1;
  531. foreach ($rows as $field => $cols) {
  532. if (is_array($cols)) {
  533. $j = 2;
  534. foreach ($cols as $col) {
  535. $cell = $table->find('css', 'tr:nth-child('.$i.') :nth-child('.$j.')');
  536. $j++;
  537. if (!$cell) {
  538. throw new Exception('Cell not found.');
  539. }
  540. if ($cell->getText() != $col) {
  541. throw new Exception('Table text not found. Found "'.$cell->getText().'" <> "'.$col.'"');
  542. }
  543. }
  544. } else {
  545. $cell = $table->find('css', 'tr:nth-child('.$i.') :nth-child(2)');
  546. if (!$cell) {
  547. throw new Exception('Cell not found.');
  548. }
  549. if ($cell->getText() != $cols) {
  550. throw new Exception('Table text not found. Found "'.$cell->getText().'" <> "'.$cols.'"');
  551. }
  552. }
  553. $i++;
  554. }
  555. }
  556. }