SettingsContext.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. /**
  3. *
  4. * @copyright Copyright (c) 2017, Daniel Calviño Sánchez (danxuliu@gmail.com)
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. use Behat\Behat\Context\Context;
  23. use PHPUnit\Framework\Assert;
  24. class SettingsContext implements Context, ActorAwareInterface {
  25. use ActorAware;
  26. /**
  27. * @return Locator
  28. */
  29. public static function acceptSharesByDefaultCheckbox() {
  30. // forThe()->checkbox("Accept user...") can not be used here; that would
  31. // return the checkbox itself, but the element that the user interacts
  32. // with is the label.
  33. return Locator::forThe()->xpath("//label[normalize-space() = 'Accept user and group shares by default']")->
  34. describedAs("Accept shares by default checkbox in Sharing section in Personal Sharing Settings");
  35. }
  36. /**
  37. * @return Locator
  38. */
  39. public static function acceptSharesByDefaultCheckboxInput() {
  40. return Locator::forThe()->checkbox("Accept user and group shares by default")->
  41. describedAs("Accept shares by default checkbox input in Sharing section in Personal Sharing Settings");
  42. }
  43. /**
  44. * @return Locator
  45. */
  46. public static function allowResharingCheckbox() {
  47. // forThe()->checkbox("Allow resharing") can not be used here; that
  48. // would return the checkbox itself, but the element that the user
  49. // interacts with is the label.
  50. return Locator::forThe()->xpath("//label[normalize-space() = 'Allow resharing']")->
  51. describedAs("Allow resharing checkbox in Sharing section in Administration Sharing Settings");
  52. }
  53. /**
  54. * @return Locator
  55. */
  56. public static function allowResharingCheckboxInput() {
  57. return Locator::forThe()->checkbox("Allow resharing")->
  58. describedAs("Allow resharing checkbox input in Sharing section in Administration Sharing Settings");
  59. }
  60. /**
  61. * @return Locator
  62. */
  63. public static function restrictUsernameAutocompletionToGroupsCheckbox() {
  64. // forThe()->checkbox("Restrict username...") can not be used here; that
  65. // would return the checkbox itself, but the element that the user
  66. // interacts with is the label.
  67. return Locator::forThe()->xpath("//label[normalize-space() = 'Allow username autocompletion to users within the same groups']")->
  68. describedAs("Allow username autocompletion to users within the same groups checkbox in Sharing section in Administration Sharing Settings");
  69. }
  70. /**
  71. * @return Locator
  72. */
  73. public static function restrictUsernameAutocompletionToGroupsCheckboxInput() {
  74. return Locator::forThe()->checkbox("Allow username autocompletion to users within the same groups")->
  75. describedAs("Allow username autocompletion to users within the same groups checkbox input in Sharing section in Administration Sharing Settings");
  76. }
  77. /**
  78. * @return Locator
  79. */
  80. public static function systemTagsSelectTagButton() {
  81. return Locator::forThe()->id("s2id_systemtag")->
  82. describedAs("Select tag button in system tags section in Administration Settings");
  83. }
  84. /**
  85. * @return Locator
  86. */
  87. public static function systemTagsItemInDropdownForTag($tag) {
  88. return Locator::forThe()->xpath("//*[contains(concat(' ', normalize-space(@class), ' '), ' select2-result-label ')]//span[normalize-space() = '$tag']/ancestor::li")->
  89. descendantOf(self::select2Dropdown())->
  90. describedAs("Item in dropdown for tag $tag in system tags section in Administration Settings");
  91. }
  92. /**
  93. * @return Locator
  94. */
  95. private static function select2Dropdown() {
  96. return Locator::forThe()->css("#select2-drop")->
  97. describedAs("Select2 dropdown in Settings");
  98. }
  99. /**
  100. * @return Locator
  101. */
  102. private static function select2DropdownMask() {
  103. return Locator::forThe()->css("#select2-drop-mask")->
  104. describedAs("Select2 dropdown mask in Settings");
  105. }
  106. /**
  107. * @return Locator
  108. */
  109. public static function systemTagsTagNameInput() {
  110. return Locator::forThe()->id("systemtag_name")->
  111. describedAs("Tag name input in system tags section in Administration Settings");
  112. }
  113. /**
  114. * @return Locator
  115. */
  116. public static function systemTagsCreateOrUpdateButton() {
  117. return Locator::forThe()->id("systemtag_submit")->
  118. describedAs("Create/Update button in system tags section in Administration Settings");
  119. }
  120. /**
  121. * @return Locator
  122. */
  123. public static function systemTagsResetButton() {
  124. return Locator::forThe()->id("systemtag_reset")->
  125. describedAs("Reset button in system tags section in Administration Settings");
  126. }
  127. /**
  128. * @When I disable accepting the shares by default
  129. */
  130. public function iDisableAcceptingTheSharesByDefault() {
  131. $this->iSeeThatSharesAreAcceptedByDefault();
  132. $this->actor->find(self::acceptSharesByDefaultCheckbox(), 2)->click();
  133. }
  134. /**
  135. * @When I disable resharing
  136. */
  137. public function iDisableResharing() {
  138. $this->iSeeThatResharingIsEnabled();
  139. $this->actor->find(self::allowResharingCheckbox(), 2)->click();
  140. }
  141. /**
  142. * @When I enable restricting username autocompletion to groups
  143. */
  144. public function iEnableRestrictingUsernameAutocompletionToGroups() {
  145. $this->iSeeThatUsernameAutocompletionIsNotRestrictedToGroups();
  146. $this->actor->find(self::restrictUsernameAutocompletionToGroupsCheckbox(), 2)->click();
  147. }
  148. /**
  149. * @When I create the tag :tag in the settings
  150. */
  151. public function iCreateTheTagInTheSettings($tag) {
  152. $this->actor->find(self::systemTagsResetButton(), 10)->click();
  153. $this->actor->find(self::systemTagsTagNameInput())->setValue($tag);
  154. $this->actor->find(self::systemTagsCreateOrUpdateButton())->click();
  155. }
  156. /**
  157. * @Then I see that shares are accepted by default
  158. */
  159. public function iSeeThatSharesAreAcceptedByDefault() {
  160. Assert::assertTrue(
  161. $this->actor->find(self::acceptSharesByDefaultCheckboxInput(), 10)->isChecked());
  162. }
  163. /**
  164. * @Then I see that resharing is enabled
  165. */
  166. public function iSeeThatResharingIsEnabled() {
  167. Assert::assertTrue(
  168. $this->actor->find(self::allowResharingCheckboxInput(), 10)->isChecked());
  169. }
  170. /**
  171. * @Then I see that resharing is disabled
  172. */
  173. public function iSeeThatResharingIsDisabled() {
  174. Assert::assertFalse(
  175. $this->actor->find(self::allowResharingCheckboxInput(), 10)->isChecked());
  176. }
  177. /**
  178. * @Then I see that username autocompletion is restricted to groups
  179. */
  180. public function iSeeThatUsernameAutocompletionIsRestrictedToGroups() {
  181. Assert::assertTrue(
  182. $this->actor->find(self::restrictUsernameAutocompletionToGroupsCheckboxInput(), 10)->isChecked());
  183. }
  184. /**
  185. * @Then I see that username autocompletion is not restricted to groups
  186. */
  187. public function iSeeThatUsernameAutocompletionIsNotRestrictedToGroups() {
  188. Assert::assertFalse(
  189. $this->actor->find(self::restrictUsernameAutocompletionToGroupsCheckboxInput(), 10)->isChecked());
  190. }
  191. /**
  192. * @Then I see that shares are not accepted by default
  193. */
  194. public function iSeeThatSharesAreNotAcceptedByDefault() {
  195. Assert::assertFalse(
  196. $this->actor->find(self::acceptSharesByDefaultCheckboxInput(), 10)->isChecked());
  197. }
  198. /**
  199. * @Then I see that the button to select tags is shown
  200. */
  201. public function iSeeThatTheButtonToSelectTagsIsShown() {
  202. Assert::assertTrue($this->actor->find(self::systemTagsSelectTagButton(), 10)->isVisible());
  203. }
  204. /**
  205. * @Then I see that the dropdown for tags in the settings eventually contains the tag :tag
  206. */
  207. public function iSeeThatTheDropdownForTagsInTheSettingsEventuallyContainsTheTag($tag) {
  208. // When the dropdown is opened it is not automatically updated if new
  209. // tags are added to the server, and when a tag is created, no explicit
  210. // feedback is provided to the user about the completion of that
  211. // operation (that is, when the tag is added to the server). Therefore,
  212. // to verify that creating a tag does in fact add it to the server it is
  213. // necessary to repeatedly open the dropdown until the tag is shown in
  214. // the dropdown (or the limit of tries is reached).
  215. Assert::assertTrue($this->actor->find(self::systemTagsSelectTagButton(), 10)->isVisible());
  216. $actor = $this->actor;
  217. $tagFoundInDropdownCallback = function () use ($actor, $tag) {
  218. // Open the dropdown to look for the tag.
  219. $actor->find(self::systemTagsSelectTagButton())->click();
  220. // When the dropdown is opened it is initially empty, and its
  221. // contents are updated once received from the server. Therefore, a
  222. // timeout must be used when looking for the tags.
  223. try {
  224. $tagFound = $this->actor->find(self::systemTagsItemInDropdownForTag($tag), 10)->isVisible();
  225. } catch (NoSuchElementException $exception) {
  226. $tagFound = false;
  227. }
  228. // Close again the dropdown after looking for the tag. When a
  229. // dropdown is opened Select2 creates a special element that masks
  230. // every other element but the dropdown to get all mouse clicks;
  231. // this is used by Select2 to close the dropdown when the user
  232. // clicks outside it.
  233. $actor->find(self::select2DropdownMask())->click();
  234. return $tagFound;
  235. };
  236. $numberOfTries = 5;
  237. for ($i = 0; $i < $numberOfTries; $i++) {
  238. if ($tagFoundInDropdownCallback()) {
  239. return;
  240. }
  241. }
  242. Assert::fail("The dropdown in system tags section in Administration Settings does not contain the tag $tag after $numberOfTries tries");
  243. }
  244. }