FeatureContext.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593
  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. * @Given /^I fill hidden field "([^"]*)" with "([^"]*)"$/
  279. */
  280. public function iFillHiddenFieldWith($field, $value)
  281. {
  282. $this->getSession()->getPage()->find(
  283. 'css',
  284. 'input[name="'.$field.'"]'
  285. )->setValue($value);
  286. }
  287. /**
  288. * @When /^(?:|I )fill in select2 input "(?P<field>(?:[^"]|\\")*)" with id "(?P<id>(?:[^"]|\\")*)" and value "(?P<value>(?:[^"]|\\")*)"$/
  289. */
  290. public function iFillInSelectInputWithAndSelect($field, $id, $value)
  291. {
  292. $this->getSession()->executeScript("$('$field').select2({data : [{id: $id, text: '$value'}]});");
  293. }
  294. /**
  295. * @When /^(?:|I )confirm the popup$/
  296. */
  297. public function confirmPopup()
  298. {
  299. // See
  300. // https://gist.github.com/blazarecki/2888851
  301. /** @var \Behat\Mink\Driver\Selenium2Driver $driver Needed because no cross-driver way yet */
  302. $this->getSession()->getDriver()->getWebDriverSession()->accept_alert();
  303. }
  304. /**
  305. * @When /^(?:|I )fill in select bootstrap input "(?P<field>(?:[^"]|\\")*)" with "(?P<value>(?:[^"]|\\")*)" and select "(?P<entry>(?:[^"]|\\")*)"$/
  306. */
  307. public function iFillInSelectBootstrapInputWithAndSelect($field, $value, $entry)
  308. {
  309. $page = $this->getSession()->getPage();
  310. $inputField = $page->find('css', $field);
  311. if (!$inputField) {
  312. throw new \Exception('No field found');
  313. }
  314. $choice = $inputField->getParent()->find('css', '.bootstrap-select');
  315. if (!$choice) {
  316. throw new \Exception('No select bootstrap choice found');
  317. }
  318. $choice->press();
  319. $selectInput = $inputField->getParent()->find('css', '.bootstrap-select .form-control');
  320. if (!$selectInput) {
  321. throw new \Exception('No input found');
  322. }
  323. $selectInput->setValue($value);
  324. $this->getSession()->wait(3000);
  325. $chosenResults = $inputField->getParent()->findAll('css', '.dropdown-menu inner li');
  326. foreach ($chosenResults as $result) {
  327. //$option = $result->find('css', '.text');
  328. if ($result->getText() == $entry) {
  329. $result->click();
  330. break;
  331. }
  332. }
  333. }
  334. /**
  335. * @When /^(?:|I )fill in select bootstrap static input "(?P<field>(?:[^"]|\\")*)" select "(?P<value>(?:[^"]|\\")*)"$/
  336. */
  337. public function iFillInSelectStaticBootstrapInputWithAndSelect($field, $value)
  338. {
  339. $this->getSession()->wait(1000);
  340. $this->getSession()->executeScript("
  341. $(function() {
  342. $('$field').selectpicker('val', '$value');
  343. });
  344. ");
  345. }
  346. /**
  347. * @When /^(?:|I )fill in select bootstrap static by text "(?P<field>(?:[^"]|\\")*)" select "(?P<value>(?:[^"]|\\")*)"$/
  348. */
  349. public function iFillInSelectStaticBootstrapInputWithAndSelectByText($field, $value)
  350. {
  351. $this->getSession()->wait(1000);
  352. $this->getSession()->executeScript("
  353. $('$field > option').each(function(index, option) {
  354. if (option.text == '$value') {
  355. $('$field').selectpicker('val', option.value);
  356. }
  357. });
  358. ");
  359. }
  360. /**
  361. * @When /^wait for the page to be loaded$/
  362. */
  363. public function waitForThePageToBeLoaded()
  364. {
  365. $this->getSession()->wait(3000);
  366. }
  367. /**
  368. * @When /^wait very long for the page to be loaded$/
  369. */
  370. public function waitVeryLongForThePageToBeLoaded()
  371. {
  372. //$this->getSession()->wait(10000, "document.readyState === 'complete'");
  373. $this->getSession()->wait(8000);
  374. }
  375. /**
  376. * @When /^I check the "([^"]*)" radio button$/
  377. */
  378. public function iCheckTheRadioButton($radioLabel)
  379. {
  380. $radioButton = $this->getSession()->getPage()->findField($radioLabel);
  381. if (null === $radioButton) {
  382. throw new Exception("Cannot find radio button ".$radioLabel);
  383. }
  384. //$value = $radioButton->getAttribute('value');
  385. $this->getSession()->getDriver()->click($radioButton->getXPath());
  386. }
  387. /**
  388. * @When /^I check radio button with label "([^"]*)"$/
  389. */
  390. public function iCheckTheRadioButtonWithLabel($label)
  391. {
  392. $this->getSession()->executeScript("
  393. $(function() {
  394. $(':contains(\$label\")').parent().find('input').prop('checked', true);
  395. });
  396. ");
  397. }
  398. /**
  399. * @When /^I press advanced settings$/
  400. */
  401. public function iSelectFromSelectWithLabel()
  402. {
  403. $this->pressButton('Advanced settings');
  404. }
  405. /**
  406. * Clicks link with specified id|title|alt|text
  407. * Example: When I follow "Log In"
  408. * Example: And I follow "Log In"
  409. *
  410. * @When /^(?:|I )focus "(?P<link>(?:[^"]|\\")*)"$/
  411. */
  412. public function focus($input)
  413. {
  414. $input = $this->getSession()->getPage()->findField($input);
  415. $input->focus();
  416. }
  417. /**
  418. * @Given /^I check the "([^"]*)" radio button with "([^"]*)" value$/
  419. */
  420. public function iCheckTheRadioButtonWithValue($element, $value)
  421. {
  422. $this->getSession()->executeScript("
  423. $(function() {
  424. $('input[type=\"radio\"][name=".$element."][value=".$value."]').prop('checked', true);
  425. });
  426. ");
  427. return true;
  428. }
  429. /**
  430. * @Then /^I should see an icon with title "([^"]*)"$/
  431. */
  432. public function iShouldSeeAnIconWithTitle($value)
  433. {
  434. $el = $this->getSession()->getPage()->find('xpath', "//img[@title='$value']");
  435. if (null === $el) {
  436. throw new Exception(
  437. 'Could not find an icon with title: '.$value
  438. );
  439. }
  440. return true;
  441. }
  442. /**
  443. * @Then /^I should not see an icon with title "([^"]*)"$/
  444. */
  445. public function iShouldNotSeeAnIconWithTitle($value)
  446. {
  447. $el = $this->getSession()->getPage()->find('xpath', "//img[@title='$value']");
  448. if (null === $el) {
  449. return true;
  450. }
  451. return false;
  452. }
  453. /**
  454. * @Then /^I save current URL with name "([^"]*)"$/
  455. */
  456. public function saveUrlWithName($name)
  457. {
  458. $url = $this->getSession()->getCurrentUrl();
  459. $this->getSession()->setCookie($name, $url);
  460. }
  461. /**
  462. * @Then /^I visit URL saved with name "([^"]*)"$/
  463. */
  464. public function visitSavedUrlWithName($name)
  465. {
  466. $url = $this->getSession()->getCookie($name);
  467. echo $url;
  468. if (empty($url)) {
  469. throw new Exception("Url with name: $name not found");
  470. }
  471. $this->visit($url);
  472. }
  473. /**
  474. * Example: Then I should see the table "#category_results":
  475. * | Categories | Absolute score | Relative score |
  476. * | Categoryname2 | 50 / 70 | 71.43% |
  477. * | Categoryname1 | 60 / 60 | 100% |
  478. *
  479. * @Then /^I should see the table "([^"]*)":$/
  480. *
  481. * @param string $tableId
  482. * @param TableNode $tableData
  483. *
  484. * @throws Exception
  485. */
  486. public function assertPageContainsTable($tableId, TableNode $tableData)
  487. {
  488. $table = $this->getSession()->getPage()->find('css', $tableId);
  489. $rows = $tableData->getRowsHash();
  490. $i = 1;
  491. $right = array_keys($rows);
  492. foreach ($right as $text) {
  493. $cell = $table->find('css', 'tr:nth-child('.$i.') :nth-child(1)');
  494. $i++;
  495. if (!$cell) {
  496. throw new Exception('Cell not found.');
  497. }
  498. if ($cell->getText() != $text) {
  499. throw new Exception('Table text not found.');
  500. }
  501. }
  502. $i = 1;
  503. foreach ($rows as $field => $cols) {
  504. if (is_array($cols)) {
  505. $j = 2;
  506. foreach ($cols as $col) {
  507. $cell = $table->find('css', 'tr:nth-child('.$i.') :nth-child('.$j.')');
  508. $j++;
  509. if (!$cell) {
  510. throw new Exception('Cell not found.');
  511. }
  512. if ($cell->getText() != $col) {
  513. throw new Exception('Table text not found. Found "'.$cell->getText().'" <> "'.$col.'"');
  514. }
  515. }
  516. } else {
  517. $cell = $table->find('css', 'tr:nth-child('.$i.') :nth-child(2)');
  518. if (!$cell) {
  519. throw new Exception('Cell not found.');
  520. }
  521. if ($cell->getText() != $cols) {
  522. throw new Exception('Table text not found. Found "'.$cell->getText().'" <> "'.$cols.'"');
  523. }
  524. }
  525. $i++;
  526. }
  527. }
  528. }