UsersSettingsContext.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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()->css('[data-test="form"]')->
  34. describedAs("New user form in Users Settings");
  35. }
  36. /**
  37. * @return Locator
  38. */
  39. public static function userNameFieldForNewUser() {
  40. return Locator::forThe()->css('[data-test="username"]')->
  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()->css('[data-test="displayName"]')->
  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()->css('[data-test="password"]')->
  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()->css('[data-test="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()->xpath("//tbody[contains(@class, 'user-list__body')]/tr[td[@data-test='$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(".vs__dropdown-option--highlight")->
  107. describedAs("Selected $cell option in $cell input for user $user in Users Settings");
  108. }
  109. /**
  110. * @return Locator
  111. */
  112. public static function actionsMenuOf($user) {
  113. return Locator::forThe()->css(".userActions .action-item:not(.action-item--single)")->
  114. descendantOf(self::rowForUser($user))->
  115. describedAs("Actions menu for user $user in Users Settings");
  116. }
  117. /**
  118. * @return Locator
  119. */
  120. public static function theAction($action, $user) {
  121. return Locator::forThe()->xpath("//button[@aria-label = normalize-space('$action')]")->
  122. describedAs("$action action for the user $user row in Users Settings");
  123. }
  124. /**
  125. * @return Locator
  126. */
  127. public static function theColumn($column) {
  128. return Locator::forThe()->xpath("//div[@class='user-list-grid']//div[normalize-space() = '$column']")->
  129. describedAs("The $column column in Users Settings");
  130. }
  131. /**
  132. * @return Locator
  133. */
  134. public static function selectedSelectOption($cell, $user) {
  135. return Locator::forThe()->css(".vs__selected .name-parts")->
  136. descendantOf(self::classCellForUser($cell, $user))->
  137. describedAs("The selected option of the $cell select for the user $user in Users Settings");
  138. }
  139. /**
  140. * @return Locator
  141. */
  142. public static function editModeToggle($user) {
  143. return Locator::forThe()->css(".userActions .action-items button:first-of-type")->
  144. descendantOf(self::rowForUser($user))->
  145. describedAs("The edit toggle button for the user $user in Users Settings");
  146. }
  147. /**
  148. * @return Locator
  149. */
  150. public static function editModeOn($user) {
  151. return Locator::forThe()->css("div.user-list-grid div.row.row--editable[data-id=$user]")->
  152. describedAs("I see the edit mode is on for the user $user in Users Settings");
  153. }
  154. /**
  155. * @When I click the New user button
  156. */
  157. public function iClickTheNewUserButton() {
  158. $this->actor->find(self::newUserButton(), 10)->click();
  159. }
  160. /**
  161. * @When I click the :action action in the :user actions menu
  162. */
  163. public function iClickTheAction($action, $user) {
  164. $this->actor->find(self::theAction($action, $user), 10)->click();
  165. }
  166. /**
  167. * @When I open the actions menu for the user :user
  168. */
  169. public function iOpenTheActionsMenuOf($user) {
  170. $this->actor->find(self::actionsMenuOf($user), 10)->click();
  171. }
  172. /**
  173. * @When I set the user name for the new user to :user
  174. */
  175. public function iSetTheUserNameForTheNewUserTo($user) {
  176. $this->actor->find(self::userNameFieldForNewUser(), 10)->setValue($user);
  177. }
  178. /**
  179. * @When I set the display name for the new user to :displayName
  180. */
  181. public function iSetTheDisplayNameForTheNewUserTo($displayName) {
  182. $this->actor->find(self::displayNameFieldForNewUser(), 10)->setValue($displayName);
  183. }
  184. /**
  185. * @When I set the password for the new user to :password
  186. */
  187. public function iSetThePasswordForTheNewUserTo($password) {
  188. $this->actor->find(self::passwordFieldForNewUser(), 10)->setValue($password);
  189. }
  190. /**
  191. * @When I create the new user
  192. */
  193. public function iCreateTheNewUser() {
  194. $this->actor->find(self::createNewUserButton(), 10)->click();
  195. }
  196. /**
  197. * @When I toggle the edit mode for the user :user
  198. */
  199. public function iToggleTheEditModeForUser($user) {
  200. $this->actor->find(self::editModeToggle($user), 10)->click();
  201. }
  202. /**
  203. * @When I create user :user with password :password
  204. */
  205. public function iCreateUserWithPassword($user, $password) {
  206. $this->actor->find(self::userNameFieldForNewUser(), 10)->setValue($user);
  207. $this->actor->find(self::passwordFieldForNewUser())->setValue($password);
  208. $this->actor->find(self::createNewUserButton())->click();
  209. }
  210. /**
  211. * @When I set the :field for :user to :value
  212. */
  213. public function iSetTheFieldForUserTo($field, $user, $value) {
  214. $this->actor->find(self::inputForUserInCell($field, $user), 2)->setValue($value . Key::ENTER);
  215. }
  216. /**
  217. * Assigning/withdrawing is the same action (it toggles).
  218. *
  219. * @When I assign the user :user to the group :group
  220. * @When I withdraw the user :user from the group :group
  221. */
  222. public function iAssignTheUserToTheGroup($user, $group) {
  223. $this->actor->find(self::inputForUserInCell('groups', $user))->setValue($group);
  224. $this->actor->find(self::optionInInputForUser('groups', $user))->click();
  225. }
  226. /**
  227. * @When I set the user :user quota to :quota
  228. */
  229. public function iSetTheUserQuotaTo($user, $quota) {
  230. $this->actor->find(self::inputForUserInCell('quota', $user))->setValue($quota);
  231. $this->actor->find(self::optionInInputForUser('quota', $user))->click();
  232. }
  233. /**
  234. * @Then I see that the list of users contains the user :user
  235. */
  236. public function iSeeThatTheListOfUsersContainsTheUser($user) {
  237. if (!WaitFor::elementToBeEventuallyShown(
  238. $this->actor,
  239. self::rowForUser($user),
  240. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  241. Assert::fail("The user $user in the list of users is not shown yet after $timeout seconds");
  242. }
  243. }
  244. /**
  245. * @Then I see that the list of users does not contains the user :user
  246. */
  247. public function iSeeThatTheListOfUsersDoesNotContainsTheUser($user) {
  248. if (!WaitFor::elementToBeEventuallyNotShown(
  249. $this->actor,
  250. self::rowForUser($user),
  251. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  252. Assert::fail("The user $user in the list of users is still shown after $timeout seconds");
  253. }
  254. }
  255. /**
  256. * @Then I see that the new user form is shown
  257. */
  258. public function iSeeThatTheNewUserFormIsShown() {
  259. if (!WaitFor::elementToBeEventuallyShown(
  260. $this->actor,
  261. self::newUserForm(),
  262. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  263. Assert::fail("The new user form is not shown yet after $timeout seconds");
  264. }
  265. }
  266. /**
  267. * @Then I see that the :action action in the :user actions menu is shown
  268. */
  269. public function iSeeTheAction($action, $user) {
  270. Assert::assertTrue(
  271. $this->actor->find(self::theAction($action, $user), 10)->isVisible());
  272. }
  273. /**
  274. * @Then I see that the :column column is shown
  275. */
  276. public function iSeeThatTheColumnIsShown($column) {
  277. Assert::assertTrue(
  278. $this->actor->find(self::theColumn($column), 10)->isVisible());
  279. }
  280. /**
  281. * @Then I see that the :field of :user is :value
  282. */
  283. public function iSeeThatTheFieldOfUserIs($field, $user, $value) {
  284. Assert::assertEquals(
  285. $this->actor->find(self::inputForUserInCell($field, $user), 10)->getValue(), $value);
  286. }
  287. /**
  288. * @Then I see that the display name for the user :user is :displayName
  289. */
  290. public function iSeeThatTheDisplayNameForTheUserIs($user, $displayName) {
  291. Assert::assertEquals(
  292. $displayName, $this->actor->find(self::displayNameCellForUser($user), 10)->getValue());
  293. }
  294. /**
  295. * @Then I see that the :cell cell for user :user is done loading
  296. */
  297. public function iSeeThatTheCellForUserIsDoneLoading($cell, $user) {
  298. // It could happen that the cell for the user was done loading and thus
  299. // the loading icon hidden again even before finding the loading icon
  300. // started. Therefore, if the loading icon could not be found it is just
  301. // assumed that it was already hidden again. Nevertheless, this check
  302. // should be done anyway to ensure that the following scenario steps are
  303. // not executed before the cell for the user was done loading.
  304. try {
  305. $this->actor->find(self::classCellForUser($cell . ' icon-loading-small', $user), 1);
  306. } catch (NoSuchElementException $exception) {
  307. 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";
  308. return;
  309. }
  310. if (!WaitFor::elementToBeEventuallyNotShown(
  311. $this->actor,
  312. self::classCellForUser($cell . ' icon-loading-small', $user),
  313. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  314. Assert::fail("The loading icon for user $user is still shown after $timeout seconds");
  315. }
  316. }
  317. /**
  318. * @Then I see that the user quota of :user is :quota
  319. */
  320. public function iSeeThatTheuserQuotaIs($user, $quota) {
  321. Assert::assertEquals(
  322. $this->actor->find(self::selectedSelectOption('quota', $user), 2)->getText(), $quota);
  323. }
  324. /**
  325. * @Then I see that the edit mode is on for user :user
  326. */
  327. public function iSeeThatTheEditModeIsOn($user) {
  328. if (!WaitFor::elementToBeEventuallyShown(
  329. $this->actor,
  330. self::editModeOn($user),
  331. $timeout = 10 * $this->actor->getFindTimeoutMultiplier())) {
  332. Assert::fail("The edit mode for user $user in the list of users is not on yet after $timeout seconds");
  333. }
  334. }
  335. }