SettingsContext.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. class SettingsContext implements Context, ActorAwareInterface {
  24. use ActorAware;
  25. /**
  26. * @return Locator
  27. */
  28. public static function systemTagsSelectTagButton() {
  29. return Locator::forThe()->id("s2id_systemtag")->
  30. describedAs("Select tag button in system tags section in Administration Settings");
  31. }
  32. /**
  33. * @return Locator
  34. */
  35. public static function systemTagsItemInDropdownForTag($tag) {
  36. return Locator::forThe()->xpath("//*[contains(concat(' ', normalize-space(@class), ' '), ' select2-result-label ')]//span[normalize-space() = '$tag']/ancestor::li")->
  37. descendantOf(self::select2Dropdown())->
  38. describedAs("Item in dropdown for tag $tag in system tags section in Administration Settings");
  39. }
  40. /**
  41. * @return Locator
  42. */
  43. private static function select2Dropdown() {
  44. return Locator::forThe()->css("#select2-drop")->
  45. describedAs("Select2 dropdown in Settings");
  46. }
  47. /**
  48. * @return Locator
  49. */
  50. private static function select2DropdownMask() {
  51. return Locator::forThe()->css("#select2-drop-mask")->
  52. describedAs("Select2 dropdown mask in Settings");
  53. }
  54. /**
  55. * @return Locator
  56. */
  57. public static function systemTagsTagNameInput() {
  58. return Locator::forThe()->id("systemtag_name")->
  59. describedAs("Tag name input in system tags section in Administration Settings");
  60. }
  61. /**
  62. * @return Locator
  63. */
  64. public static function systemTagsCreateOrUpdateButton() {
  65. return Locator::forThe()->id("systemtag_submit")->
  66. describedAs("Create/Update button in system tags section in Administration Settings");
  67. }
  68. /**
  69. * @return Locator
  70. */
  71. public static function systemTagsResetButton() {
  72. return Locator::forThe()->id("systemtag_reset")->
  73. describedAs("Reset button in system tags section in Administration Settings");
  74. }
  75. /**
  76. * @When I create the tag :tag in the settings
  77. */
  78. public function iCreateTheTagInTheSettings($tag) {
  79. $this->actor->find(self::systemTagsResetButton(), 10)->click();
  80. $this->actor->find(self::systemTagsTagNameInput())->setValue($tag);
  81. $this->actor->find(self::systemTagsCreateOrUpdateButton())->click();
  82. }
  83. /**
  84. * @Then I see that the button to select tags is shown
  85. */
  86. public function iSeeThatTheButtonToSelectTagsIsShown() {
  87. PHPUnit_Framework_Assert::assertTrue($this->actor->find(self::systemTagsSelectTagButton(), 10)->isVisible());
  88. }
  89. /**
  90. * @Then I see that the dropdown for tags in the settings eventually contains the tag :tag
  91. */
  92. public function iSeeThatTheDropdownForTagsInTheSettingsEventuallyContainsTheTag($tag) {
  93. // When the dropdown is opened it is not automatically updated if new
  94. // tags are added to the server, and when a tag is created, no explicit
  95. // feedback is provided to the user about the completion of that
  96. // operation (that is, when the tag is added to the server). Therefore,
  97. // to verify that creating a tag does in fact add it to the server it is
  98. // necessary to repeatedly open the dropdown until the tag is shown in
  99. // the dropdown (or the limit of tries is reached).
  100. PHPUnit_Framework_Assert::assertTrue($this->actor->find(self::systemTagsSelectTagButton(), 10)->isVisible());
  101. $actor = $this->actor;
  102. $tagFoundInDropdownCallback = function() use($actor, $tag) {
  103. // Open the dropdown to look for the tag.
  104. $actor->find(self::systemTagsSelectTagButton())->click();
  105. // When the dropdown is opened it is initially empty, and its
  106. // contents are updated once received from the server. Therefore, a
  107. // timeout must be used when looking for the tags.
  108. try {
  109. $tagFound = $this->actor->find(self::systemTagsItemInDropdownForTag($tag), 10)->isVisible();
  110. } catch (NoSuchElementException $exception) {
  111. $tagFound = false;
  112. }
  113. // Close again the dropdown after looking for the tag. When a
  114. // dropdown is opened Select2 creates a special element that masks
  115. // every other element but the dropdown to get all mouse clicks;
  116. // this is used by Select2 to close the dropdown when the user
  117. // clicks outside it.
  118. $actor->find(self::select2DropdownMask())->click();
  119. return $tagFound;
  120. };
  121. $numberOfTries = 5;
  122. for ($i = 0; $i < $numberOfTries; $i++) {
  123. if ($tagFoundInDropdownCallback()) {
  124. return;
  125. }
  126. }
  127. PHPUnit_Framework_Assert::fail("The dropdown in system tags section in Administration Settings does not contain the tag $tag after $numberOfTries tries");
  128. }
  129. }