PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/repository/tests/behat/behat_filepicker.php

http://github.com/moodle/moodle
PHP | 310 lines | 113 code | 48 blank | 149 comment | 3 complexity | cae0ebbb820841a85580ec0a8d075845 MD5 | raw file
Possible License(s): MIT, AGPL-3.0, MPL-2.0-no-copyleft-exception, LGPL-3.0, GPL-3.0, Apache-2.0, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. // This file is part of Moodle - http://moodle.org/
  3. //
  4. // Moodle is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // Moodle is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Filemanager and filepicker manipulation steps definitions.
  18. *
  19. * @package core_filepicker
  20. * @category test
  21. * @copyright 2013 David MonllaĆ³
  22. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23. */
  24. // NOTE: no MOODLE_INTERNAL test here, this file may be required by behat before including /config.php.
  25. require_once(__DIR__ . '/../../../lib/behat/core_behat_file_helper.php');
  26. use Behat\Mink\Exception\ExpectationException as ExpectationException,
  27. Behat\Gherkin\Node\TableNode as TableNode;
  28. /**
  29. * Steps definitions to deal with the filemanager and filepicker.
  30. *
  31. * @package core_filepicker
  32. * @category test
  33. * @copyright 2013 David MonllaĆ³
  34. * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35. */
  36. class behat_filepicker extends behat_base {
  37. use core_behat_file_helper;
  38. /**
  39. * Creates a folder with specified name in the current folder and in the specified filemanager field.
  40. *
  41. * @Given /^I create "(?P<foldername_string>(?:[^"]|\\")*)" folder in "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
  42. * @throws ExpectationException Thrown by behat_base::find
  43. * @param string $foldername
  44. * @param string $filemanagerelement
  45. */
  46. public function i_create_folder_in_filemanager($foldername, $filemanagerelement) {
  47. $fieldnode = $this->get_filepicker_node($filemanagerelement);
  48. // Looking for the create folder button inside the specified filemanager.
  49. $exception = new ExpectationException('No folders can be created in "'.$filemanagerelement.'" filemanager',
  50. $this->getSession());
  51. $newfolder = $this->find('css', 'div.fp-btn-mkdir a', $exception, $fieldnode);
  52. $newfolder->click();
  53. // Setting the folder name in the modal window.
  54. $exception = new ExpectationException('The dialog to enter the folder name does not appear', $this->getSession());
  55. $dialoginput = $this->find('css', '.fp-mkdir-dlg-text input', $exception);
  56. $dialoginput->setValue($foldername);
  57. $exception = new ExpectationException('The button for the create folder dialog can not be located', $this->getSession());
  58. $dialognode = $this->find('css', '.moodle-dialogue-focused');
  59. $buttonnode = $this->find('css', '.fp-dlg-butcreate', $exception, $dialognode);
  60. $buttonnode->click();
  61. }
  62. /**
  63. * Opens the contents of a filemanager folder. It looks for the folder in the current folder and in the path bar.
  64. *
  65. * @Given /^I open "(?P<foldername_string>(?:[^"]|\\")*)" folder from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
  66. * @throws ExpectationException Thrown by behat_base::find
  67. * @param string $foldername
  68. * @param string $filemanagerelement
  69. */
  70. public function i_open_folder_from_filemanager($foldername, $filemanagerelement) {
  71. $fieldnode = $this->get_filepicker_node($filemanagerelement);
  72. $exception = new ExpectationException(
  73. 'The "'.$foldername.'" folder can not be found in the "'.$filemanagerelement.'" filemanager',
  74. $this->getSession()
  75. );
  76. $folderliteral = behat_context_helper::escape($foldername);
  77. // We look both in the pathbar and in the contents.
  78. try {
  79. // In the current folder workspace.
  80. $folder = $this->find(
  81. 'xpath',
  82. "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-folder ')]" .
  83. "/descendant::div[contains(concat(' ', normalize-space(@class), ' '), ' fp-filename ')]" .
  84. "[normalize-space(.)=$folderliteral]",
  85. $exception,
  86. $fieldnode
  87. );
  88. } catch (ExpectationException $e) {
  89. // And in the pathbar.
  90. $folder = $this->find(
  91. 'xpath',
  92. "//a[contains(concat(' ', normalize-space(@class), ' '), ' fp-path-folder-name ')]" .
  93. "[normalize-space(.)=$folderliteral]",
  94. $exception,
  95. $fieldnode
  96. );
  97. }
  98. // It should be a NodeElement, otherwise an exception would have been thrown.
  99. $folder->click();
  100. }
  101. /**
  102. * Unzips the specified file from the specified filemanager field. The zip file has to be visible in the current folder.
  103. *
  104. * @Given /^I unzip "(?P<filename_string>(?:[^"]|\\")*)" file from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
  105. * @throws ExpectationException Thrown by behat_base::find
  106. * @param string $filename
  107. * @param string $filemanagerelement
  108. */
  109. public function i_unzip_file_from_filemanager($filename, $filemanagerelement) {
  110. // Open the contextual menu of the filemanager element.
  111. $this->open_element_contextual_menu($filename, $filemanagerelement);
  112. // Execute the action.
  113. $exception = new ExpectationException($filename.' element can not be unzipped', $this->getSession());
  114. $this->perform_on_element('unzip', $exception);
  115. }
  116. /**
  117. * Zips the specified folder from the specified filemanager field. The folder has to be in the current folder.
  118. *
  119. * @Given /^I zip "(?P<filename_string>(?:[^"]|\\")*)" folder from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
  120. * @throws ExpectationException Thrown by behat_base::find
  121. * @param string $foldername
  122. * @param string $filemanagerelement
  123. */
  124. public function i_zip_folder_from_filemanager($foldername, $filemanagerelement) {
  125. // Open the contextual menu of the filemanager element.
  126. $this->open_element_contextual_menu($foldername, $filemanagerelement);
  127. // Execute the action.
  128. $exception = new ExpectationException($foldername.' element can not be zipped', $this->getSession());
  129. $this->perform_on_element('zip', $exception);
  130. }
  131. /**
  132. * Deletes the specified file or folder from the specified filemanager field.
  133. *
  134. * @Given /^I delete "(?P<file_or_folder_name_string>(?:[^"]|\\")*)" from "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager$/
  135. * @throws ExpectationException Thrown by behat_base::find
  136. * @param string $name
  137. * @param string $filemanagerelement
  138. */
  139. public function i_delete_file_from_filemanager($name, $filemanagerelement) {
  140. // Open the contextual menu of the filemanager element.
  141. $this->open_element_contextual_menu($name, $filemanagerelement);
  142. // Execute the action.
  143. $exception = new ExpectationException($name.' element can not be deleted', $this->getSession());
  144. $this->perform_on_element('delete', $exception);
  145. // Yes, we are sure.
  146. // Using xpath + click instead of pressButton as 'Ok' it is a common string.
  147. $okbutton = $this->find('css', 'div.fp-dlg button.fp-dlg-butconfirm');
  148. $okbutton->click();
  149. }
  150. /**
  151. * Makes sure user can see the exact number of elements (files in folders) in the filemanager.
  152. *
  153. * @Then /^I should see "(?P<elementscount_number>\d+)" elements in "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
  154. * @throws ExpectationException Thrown by behat_base::find
  155. * @param int $elementscount
  156. * @param string $filemanagerelement
  157. */
  158. public function i_should_see_elements_in_filemanager($elementscount, $filemanagerelement) {
  159. $filemanagernode = $this->get_filepicker_node($filemanagerelement);
  160. // We count .fp-file elements inside a filemanager not being updated.
  161. $xpath = "//div[contains(concat(' ', normalize-space(@class), ' '), ' filemanager ')]" .
  162. "[not(contains(concat(' ', normalize-space(@class), ' '), ' fm-updating '))]" .
  163. "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-content ')]" .
  164. "//div[contains(concat(' ', normalize-space(@class), ' '), ' fp-file ')]";
  165. $elements = $this->find_all('xpath', $xpath, false, $filemanagernode);
  166. if (count($elements) != $elementscount) {
  167. throw new ExpectationException('Found '.count($elements).' elements in filemanager. Expected '.$elementscount,
  168. $this->getSession());
  169. }
  170. }
  171. /**
  172. * Picks the file from repository leaving default values in select file dialogue.
  173. *
  174. * @When /^I add "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
  175. * @throws ExpectationException Thrown by behat_base::find
  176. * @param string $filepath
  177. * @param string $repository
  178. * @param string $filemanagerelement
  179. */
  180. public function i_add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement) {
  181. $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, new TableNode(array()), false);
  182. }
  183. /**
  184. * Picks the file from repository leaving default values in select file dialogue and confirming to overwrite an existing file.
  185. *
  186. * @When /^I add and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanagerelement_string>(?:[^"]|\\")*)" filemanager$/
  187. * @throws ExpectationException Thrown by behat_base::find
  188. * @param string $filepath
  189. * @param string $repository
  190. * @param string $filemanagerelement
  191. */
  192. public function i_add_and_overwrite_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement) {
  193. $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, new TableNode(array()),
  194. get_string('overwrite', 'repository'));
  195. }
  196. /**
  197. * Picks the file from repository filling the form in Select file dialogue.
  198. *
  199. * @When /^I add "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
  200. * @throws ExpectationException Thrown by behat_base::find
  201. * @param string $filepath
  202. * @param string $repository
  203. * @param string $filemanagerelement
  204. * @param TableNode $data Data to fill the form in Select file dialogue
  205. */
  206. public function i_add_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement, TableNode $data) {
  207. $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data, false);
  208. }
  209. /**
  210. * Picks the file from repository confirming to overwrite an existing file
  211. *
  212. * @When /^I add and overwrite "(?P<filepath_string>(?:[^"]|\\")*)" file from "(?P<repository_string>(?:[^"]|\\")*)" to "(?P<filemanager_field_string>(?:[^"]|\\")*)" filemanager as:$/
  213. * @throws ExpectationException Thrown by behat_base::find
  214. * @param string $filepath
  215. * @param string $repository
  216. * @param string $filemanagerelement
  217. * @param TableNode $data Data to fill the form in Select file dialogue
  218. */
  219. public function i_add_and_overwrite_file_from_repository_to_filemanager_as($filepath, $repository, $filemanagerelement,
  220. TableNode $data) {
  221. $this->add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, $data,
  222. get_string('overwrite', 'repository'));
  223. }
  224. /**
  225. * Picks the file from private files repository
  226. *
  227. * @throws ExpectationException Thrown by behat_base::find
  228. * @param string $filepath
  229. * @param string $repository
  230. * @param string $filemanagerelement
  231. * @param TableNode $data Data to fill the form in Select file dialogue
  232. * @param false|string $overwriteaction false if we don't expect that file with the same name already exists,
  233. * or button text in overwrite dialogue ("Overwrite", "Rename to ...", "Cancel")
  234. */
  235. protected function add_file_from_repository_to_filemanager($filepath, $repository, $filemanagerelement, TableNode $data,
  236. $overwriteaction = false) {
  237. $filemanagernode = $this->get_filepicker_node($filemanagerelement);
  238. // Opening the select repository window and selecting the upload repository.
  239. $this->open_add_file_window($filemanagernode, $repository);
  240. $this->open_element_contextual_menu($filepath);
  241. // Fill the form in Select window.
  242. $datahash = $data->getRowsHash();
  243. // The action depends on the field type.
  244. foreach ($datahash as $locator => $value) {
  245. $field = behat_field_manager::get_form_field_from_label($locator, $this);
  246. // Delegates to the field class.
  247. $field->set_value($value);
  248. }
  249. $selectfilebutton = $this->find_button(get_string('getfile', 'repository'));
  250. $this->ensure_node_is_visible($selectfilebutton);
  251. $selectfilebutton->click();
  252. // We wait for all the JS to finish as it is performing an action.
  253. $this->getSession()->wait(self::get_timeout(), self::PAGE_READY_JS);
  254. if ($overwriteaction !== false) {
  255. $overwritebutton = $this->find_button($overwriteaction);
  256. $this->ensure_node_is_visible($overwritebutton);
  257. $overwritebutton->click();
  258. // We wait for all the JS to finish.
  259. $this->getSession()->wait(self::get_timeout(), self::PAGE_READY_JS);
  260. }
  261. }
  262. }