UsersSettingsContext.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. * @copyright Copyright (c) 2018, John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  6. * @copyright Copyright (c) 2019, Greta Doci <gretadoci@gmail.com>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. use Behat\Behat\Context\Context;
  25. use PHPUnit\Framework\Assert;
  26. use WebDriver\Key;
  27. class UsersSettingsContext implements Context, ActorAwareInterface {
  28. use ActorAware;
  29. /**
  30. * @return Locator
  31. */
  32. public static function newUserForm() {
  33. return Locator::forThe()->id("new-user")->
  34. describedAs("New user form in Users Settings");
  35. }
  36. /**
  37. * @return Locator
  38. */
  39. public static function userNameFieldForNewUser() {
  40. return Locator::forThe()->field("newusername")->
  41. describedAs("User name field for new user in Users Settings");
  42. }
  43. /**
  44. * @return Locator
  45. */
  46. public static function displayNameFieldForNewUser() {
  47. return Locator::forThe()->field("newdisplayname")->
  48. describedAs("Display name field for new user in Users Settings");
  49. }
  50. /**
  51. * @return Locator
  52. */
  53. public static function passwordFieldForNewUser() {
  54. return Locator::forThe()->field("newuserpassword")->
  55. describedAs("Password field for new user in Users Settings");
  56. }
  57. /**
  58. * @return Locator
  59. */
  60. public static function newUserButton() {
  61. return Locator::forThe()->id("new-user-button")->
  62. describedAs("New user button in Users Settings");
  63. }
  64. /**
  65. * @return Locator
  66. */
  67. public static function createNewUserButton() {
  68. return Locator::forThe()->xpath("//form[@id = 'new-user']//button[@type = 'submit']")->
  69. describedAs("Create user button in Users Settings");
  70. }
  71. /**
  72. * @return Locator
  73. */
  74. public static function rowForUser($user) {
  75. return Locator::forThe()->css("div.user-list-grid div.row[data-id=$user]")->
  76. describedAs("Row for user $user in Users Settings");
  77. }
  78. /**
  79. * Warning: you need to watch out for the proper classes order
  80. *
  81. * @return Locator
  82. */
  83. public static function classCellForUser($class, $user) {
  84. return Locator::forThe()->xpath("//*[contains(concat(' ', normalize-space(@class), ' '), ' $class ')]")->
  85. descendantOf(self::rowForUser($user))->
  86. describedAs("$class cell for user $user in Users Settings");
  87. }
  88. /**
  89. * @return Locator
  90. */
  91. public static function inputForUserInCell($cell, $user) {
  92. return Locator::forThe()->css("input")->
  93. descendantOf(self::classCellForUser($cell, $user))->
  94. describedAs("$cell input for user $user in Users Settings");
  95. }
  96. /**
  97. * @return Locator
  98. */
  99. public static function displayNameCellForUser($user) {
  100. return self::inputForUserInCell("displayName", $user);
  101. }
  102. /**
  103. * @return Locator
  104. */
  105. public static function optionInInputForUser($cell, $user) {
  106. return Locator::forThe()->css(".multiselect__option--highlight")->
  107. descendantOf(self::classCellForUser($cell, $user))->
  108. describedAs("Selected $cell option in $cell input for user $user in Users Settings");
  109. }
  110. /**
  111. * @return Locator
  112. */
  113. public static function actionsMenuOf($user) {
  114. return Locator::forThe()->css(".icon-more")->
  115. descendantOf(self::rowForUser($user))->
  116. describedAs("Actions menu for user $user in Users Settings");
  117. }
  118. /**
  119. * @return Locator
  120. */
  121. public static function theAction($action, $user) {
  122. return Locator::forThe()->xpath("//button[normalize-space() = '$action']")->
  123. descendantOf(self::rowForUser($user))->
  124. describedAs("$action action for the user $user row in Users Settings");
  125. }
  126. /**
  127. * @return Locator
  128. */
  129. public static function theColumn($column) {
  130. return Locator::forThe()->xpath("//div[@class='user-list-grid']//div[normalize-space() = '$column']")->
  131. describedAs("The $column column in Users Settings");
  132. }
  133. /**
  134. * @return Locator
  135. */
  136. public static function selectedSelectOption($cell, $user) {
  137. return Locator::forThe()->css(".multiselect__single")->
  138. descendantOf(self::classCellForUser($cell, $user))->
  139. describedAs("The selected option of the $cell select for the user $user in Users Settings");
  140. }
  141. /**
  142. * @return Locator
  143. */
  144. public static function editModeToggle($user) {
  145. return Locator::forThe()->css(".toggleUserActions button")->
  146. descendantOf(self::rowForUser($user))->
  147. describedAs("The edit toggle button for the user $user in Users Settings");
  148. }
  149. /**
  150. * @return Locator
  151. */
  152. public static function editModeOn($user) {
  153. return Locator::forThe()->css("div.user-list-grid div.row.row--editable[data-id=$user]")->
  154. describedAs("I see the edit mode is on for the user $user in Users Settings");
  155. }
  156. /**
  157. * @When I click the New user button
  158. */
  159. public function iClickTheNewUserButton() {
  160. $this->actor->find(self::newUserButton(), 10)->click();
  161. }
  162. /**
  163. * @When I click the :action action in the :user actions menu
  164. */
  165. public function iClickTheAction($action, $user) {
  166. $this->actor->find(self::theAction($action, $user), 10)->click();
  167. }
  168. /**
  169. * @When I open the actions menu for the user :user
  170. */
  171. public function iOpenTheActionsMenuOf($user) {
  172. $this->actor->find(self::actionsMenuOf($user), 10)->click();
  173. }
  174. /**
  175. * @When I set the user name for the new user to :user
  176. */
  177. public function iSetTheUserNameForTheNewUserTo($user) {
  178. $this->actor->find(self::userNameFieldForNewUser(), 10)->setValue($user);
  179. }
  180. /**
  181. * @When I set the display name for the new user to :displayName
  182. */
  183. public function iSetTheDisplayNameForTheNewUserTo($displayName) {
  184. $this->actor->find(self::displayNameFieldForNewUser(), 10)->setValue($displayName);
  185. }
  186. /**
  187. * @When I set the password for the new user to :password
  188. */
  189. public function iSetThePasswordForTheNewUserTo($password) {
  190. $this->actor->find(self::passwordFieldForNewUser(), 10)->setValue($password);
  191. }
  192. /**
  193. * @When I create the new user
  194. */
  195. public function iCreateTheNewUser() {
  196. $this->actor->find(self::createNewUserButton(), 10)->click();
  197. }
  198. /**
  199. * @When I toggle the edit mode for the user :user
  200. */
  201. public function iToggleTheEditModeForUser($user) {
  202. $this->actor->find(self::editModeToggle($user), 10)->click();
  203. }
  204. /**
  205. * @When I create user :user with password :password
  206. */
  207. public function iCreateUserWithPassword($user, $password) {
  208. $this->actor->find(self::userNameFieldForNewUser(), 10)->setValue($user);
  209. $this->actor->find(self::passwordFieldForNewUser())->setValue($password);
  210. $this->actor->find(self::createNewUserButton())->click();
  211. }
  212. /**
  213. * @When I set the :field for :user to :value
  214. */
  215. public function iSetTheFieldForUserTo($field, $user, $value) {
  216. $this->actor->find(self::inputForUserInCell($field, $user), 2)->setValue($value . Key::ENTER);
  217. }
  218. /**
  219. * Assigning/withdrawing is the same action (it toggles).
  220. *
  221. * @When I assign the user :user to the group :group
  222. * @When I withdraw the user :user from the group :group
  223. */
  224. public function iAssignTheUserToTheGroup($user, $group) {
  225. $this->actor->find(self::inputForUserInCell('groups', $user))->setValue($group);
  226. $this->actor->find(self::optionInInputForUser('groups', $user))->click();
  227. }
  228. /**
  229. * @When I set the user :user quota to :quota
  230. */
  231. public function iSetTheUserQuotaTo($user, $quota) {
  232. $this->actor->find(self::inputForUserInCell('quota', $user))->setValue($quota);
  233. $this->actor->find(self::optionInInputForUser('quota', $user))->click();
  234. }
  235. /**
  236. * @Then I see that the list of users contains the user :user
  237. */
  238. public function iSeeThatTheListOfUsersContainsTheUser($user) {
  239. if (!WaitFor::elementToBeEventuallyShown(
  240. $this->actor,
  241. self::rowForUser($user),
  242. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  243. Assert::fail("The user $user in the list of users is not shown yet after $timeout seconds");
  244. }
  245. }
  246. /**
  247. * @Then I see that the list of users does not contains the user :user
  248. */
  249. public function iSeeThatTheListOfUsersDoesNotContainsTheUser($user) {
  250. if (!WaitFor::elementToBeEventuallyNotShown(
  251. $this->actor,
  252. self::rowForUser($user),
  253. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  254. Assert::fail("The user $user in the list of users is still shown after $timeout seconds");
  255. }
  256. }
  257. /**
  258. * @Then I see that the new user form is shown
  259. */
  260. public function iSeeThatTheNewUserFormIsShown() {
  261. if (!WaitFor::elementToBeEventuallyShown(
  262. $this->actor,
  263. self::newUserForm(),
  264. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  265. Assert::fail("The new user form is not shown yet after $timeout seconds");
  266. }
  267. }
  268. /**
  269. * @Then I see that the :action action in the :user actions menu is shown
  270. */
  271. public function iSeeTheAction($action, $user) {
  272. Assert::assertTrue(
  273. $this->actor->find(self::theAction($action, $user), 10)->isVisible());
  274. }
  275. /**
  276. * @Then I see that the :column column is shown
  277. */
  278. public function iSeeThatTheColumnIsShown($column) {
  279. Assert::assertTrue(
  280. $this->actor->find(self::theColumn($column), 10)->isVisible());
  281. }
  282. /**
  283. * @Then I see that the :field of :user is :value
  284. */
  285. public function iSeeThatTheFieldOfUserIs($field, $user, $value) {
  286. Assert::assertEquals(
  287. $this->actor->find(self::inputForUserInCell($field, $user), 10)->getValue(), $value);
  288. }
  289. /**
  290. * @Then I see that the display name for the user :user is :displayName
  291. */
  292. public function iSeeThatTheDisplayNameForTheUserIs($user, $displayName) {
  293. Assert::assertEquals(
  294. $displayName, $this->actor->find(self::displayNameCellForUser($user), 10)->getValue());
  295. }
  296. /**
  297. * @Then I see that the :cell cell for user :user is done loading
  298. */
  299. public function iSeeThatTheCellForUserIsDoneLoading($cell, $user) {
  300. // It could happen that the cell for the user was done loading and thus
  301. // the loading icon hidden again even before finding the loading icon
  302. // started. Therefore, if the loading icon could not be found it is just
  303. // assumed that it was already hidden again. Nevertheless, this check
  304. // should be done anyway to ensure that the following scenario steps are
  305. // not executed before the cell for the user was done loading.
  306. try {
  307. $this->actor->find(self::classCellForUser($cell . ' icon-loading-small', $user), 1);
  308. } catch (NoSuchElementException $exception) {
  309. echo "The loading icon for user $user was not found after " . (1 * $this->actor->getFindTimeoutMultiplier()) . " seconds, assumming that it was shown and hidden again before the check started and continuing";
  310. return;
  311. }
  312. if (!WaitFor::elementToBeEventuallyNotShown(
  313. $this->actor,
  314. self::classCellForUser($cell . ' icon-loading-small', $user),
  315. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  316. Assert::fail("The loading icon for user $user is still shown after $timeout seconds");
  317. }
  318. }
  319. /**
  320. * @Then I see that the user quota of :user is :quota
  321. */
  322. public function iSeeThatTheuserQuotaIs($user, $quota) {
  323. Assert::assertEquals(
  324. $this->actor->find(self::selectedSelectOption('quota', $user), 2)->getText(), $quota);
  325. }
  326. /**
  327. * @Then I see that the edit mode is on for user :user
  328. */
  329. public function iSeeThatTheEditModeIsOn($user) {
  330. if (!WaitFor::elementToBeEventuallyShown(
  331. $this->actor,
  332. self::editModeOn($user),
  333. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  334. Assert::fail("The edit mode for user $user in the list of users is not on yet after $timeout seconds");
  335. }
  336. }
  337. }