ThemingAppContext.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 ThemingAppContext implements Context, ActorAwareInterface {
  25. use ActorAware;
  26. /**
  27. * @return Locator
  28. */
  29. public static function inputFieldFor($parameterName) {
  30. return Locator::forThe()->css("input")->
  31. descendantOf(self::parameterDivFor($parameterName))->
  32. describedAs("Input field for $parameterName parameter in Theming app");
  33. }
  34. /**
  35. * @return Locator
  36. */
  37. public static function resetButtonFor($parameterName) {
  38. return Locator::forThe()->css(".theme-undo")->
  39. descendantOf(self::parameterDivFor($parameterName))->
  40. describedAs("Reset button for $parameterName parameter in Theming app");
  41. }
  42. /**
  43. * @return Locator
  44. */
  45. private static function parameterDivFor($parameterName) {
  46. return Locator::forThe()->xpath("//*[@id='theming']//label//*[normalize-space() = '$parameterName']/ancestor::div[1]")->
  47. describedAs("Div for $parameterName parameter in Theming app");
  48. }
  49. /**
  50. * @return Locator
  51. */
  52. public static function statusMessage() {
  53. return Locator::forThe()->id("theming_settings_msg")->
  54. describedAs("Status message in Theming app");
  55. }
  56. /**
  57. * @When I set the :parameterName parameter in the Theming app to :parameterValue
  58. */
  59. public function iSetTheParameterInTheThemingAppTo($parameterName, $parameterValue) {
  60. $this->actor->find(self::inputFieldFor($parameterName), 10)->setValue($parameterValue);
  61. }
  62. /**
  63. * @When I reset the :parameterName parameter in the Theming app to its default value
  64. */
  65. public function iSetTheParameterInTheThemingAppToItsDefaultValue($parameterName) {
  66. // The reset button is not shown when the cursor is outside the input
  67. // field, so ensure that the cursor is on the input field by clicking on
  68. // it.
  69. $this->actor->find(self::inputFieldFor($parameterName), 10)->click();
  70. $this->actor->find(self::resetButtonFor($parameterName), 10)->click();
  71. }
  72. /**
  73. * @Then I see that the color selector in the Theming app has loaded
  74. */
  75. public function iSeeThatTheColorSelectorInTheThemingAppHasLoaded() {
  76. // Checking if the color selector has loaded by getting the background color
  77. // of the input element. If the value present in the element matches the
  78. // background of the input element, it means the color element has been
  79. // initialized.
  80. Assert::assertTrue($this->actor->find(self::inputFieldFor("Color"), 10)->isVisible());
  81. $actor = $this->actor;
  82. $colorSelectorLoadedCallback = function () use ($actor) {
  83. $colorSelectorValue = $this->getRGBArray($actor->getSession()->evaluateScript("return $('#admin-theming-color').text().trim();"));
  84. $inputBgColorRgb = $this->getRGBArray($actor->getSession()->evaluateScript("return $('#admin-theming-color').css('background-color');"));
  85. $matches = [];
  86. preg_match_all('/\d+/', $inputBgColorRgb, $matches);
  87. $inputBgColorHex = sprintf("#%02x%02x%02x", $matches[0][0], $matches[0][1], $matches[0][2]);
  88. if ($colorSelectorValue == $inputBgColorHex) {
  89. return true;
  90. }
  91. return false;
  92. };
  93. if (!Utils::waitFor($colorSelectorLoadedCallback, $timeout = 10 * $this->actor->getFindTimeoutMultiplier(), $timeoutStep = 1)) {
  94. Assert::fail("The color selector in Theming app has not been loaded after $timeout seconds");
  95. }
  96. }
  97. private function getRGBArray($color) {
  98. if (preg_match("/rgb\(\s*(\d+),\s*(\d+),\s*(\d+)\)/", $color, $matches)) {
  99. // Already an RGB (R, G, B) color
  100. // Convert from "rgb(R, G, B)" string to RGB array
  101. $tmpColor = array_splice($matches, 1);
  102. } elseif ($color[0] === '#') {
  103. $color = substr($color, 1);
  104. // HEX Color, convert to RGB array.
  105. $tmpColor = sscanf($color, "%02X%02X%02X");
  106. } else {
  107. Assert::fail("The acceptance test does not know how to handle the color string : '$color'. "
  108. . "Please provide # before HEX colors in your features.");
  109. }
  110. return $tmpColor;
  111. }
  112. /**
  113. * @Then I see that the primary color is eventually :color
  114. */
  115. public function iSeeThatThePrimaryColorIsEventually($color) {
  116. $primaryColorMatchesCallback = function () use ($color) {
  117. $primaryColor = $this->actor->getSession()->evaluateScript("return getComputedStyle(document.documentElement).getPropertyValue('--color-primary').trim();");
  118. $primaryColor = $this->getRGBArray($primaryColor);
  119. $color = $this->getRGBArray($color);
  120. return $primaryColor == $color;
  121. };
  122. if (!Utils::waitFor($primaryColorMatchesCallback, $timeout = 10 * $this->actor->getFindTimeoutMultiplier(), $timeoutStep = 1)) {
  123. Assert::fail("The primary color is not $color yet after $timeout seconds");
  124. }
  125. }
  126. /**
  127. * @Then I see that the non-plain background color variable is eventually :color
  128. */
  129. public function iSeeThatTheNonPlainBackgroundColorVariableIsEventually($color) {
  130. $colorVariableMatchesCallback = function () use ($color) {
  131. $colorVariable = $this->actor->getSession()->evaluateScript("return getComputedStyle(document.documentElement).getPropertyValue('--color-primary-default').trim();");
  132. $colorVariable = $this->getRGBArray($colorVariable);
  133. $color = $this->getRGBArray($color);
  134. return $colorVariable == $color;
  135. };
  136. if (!Utils::waitFor($colorVariableMatchesCallback, $timeout = 10 * $this->actor->getFindTimeoutMultiplier(), $timeoutStep = 1)) {
  137. Assert::fail("The non-plain background color variable is not $color yet after $timeout seconds");
  138. }
  139. }
  140. /**
  141. * @Then I see that the parameters in the Theming app are eventually saved
  142. */
  143. public function iSeeThatTheParametersInTheThemingAppAreEventuallySaved() {
  144. Assert::assertTrue($this->actor->find(self::statusMessage(), 10)->isVisible());
  145. $actor = $this->actor;
  146. $savedStatusMessageShownCallback = function () use ($actor) {
  147. if ($actor->find(self::statusMessage())->getText() !== "Saved") {
  148. return false;
  149. }
  150. return true;
  151. };
  152. if (!Utils::waitFor($savedStatusMessageShownCallback, $timeout = 10 * $this->actor->getFindTimeoutMultiplier(), $timeoutStep = 1)) {
  153. Assert::fail("The 'Saved' status messages in Theming app has not been shown after $timeout seconds");
  154. }
  155. }
  156. }