PageRenderTime 28ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/dev/tests/functional/tests/app/Mage/Review/Test/Block/Product/View/Review.php

https://gitlab.com/LisovyiEvhenii/ismextensions
PHP | 257 lines | 101 code | 24 blank | 132 comment | 2 complexity | 438f858684937f5da2b14a361c3d95fe MD5 | raw file
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magento.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magento.com for more information.
  20. *
  21. * @category Tests
  22. * @package Tests_Functional
  23. * @copyright Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. namespace Mage\Review\Test\Block\Product\View;
  27. use Magento\Mtf\Client\ElementInterface;
  28. use Magento\Mtf\Block\Block;
  29. use Mage\Review\Test\Fixture\Review as ReviewFixture;
  30. use Magento\Mtf\Client\Locator;
  31. /**
  32. * Product review block on the product page.
  33. */
  34. class Review extends Block
  35. {
  36. /**
  37. * Selector for add review link.
  38. *
  39. * @var string
  40. */
  41. protected $addReviewLink = 'a[href$="#review-form"]';
  42. /**
  43. * Review items selector.
  44. *
  45. * @var string
  46. */
  47. protected $itemsSelector = 'dl dt';
  48. /**
  49. * Item row review selector.
  50. *
  51. * @var string
  52. */
  53. protected $itemRowSelector = './/dl/dt[a[contains(text(),"%s")]]/following-sibling::dd[1]';
  54. /**
  55. * Item review author.
  56. *
  57. * @var string
  58. */
  59. protected $itemAuthor = '/span';
  60. /**
  61. * Ratings selector.
  62. *
  63. * @var string
  64. */
  65. protected $itemRatings = '//*[@class="ratings-table"]//tr';
  66. /**
  67. * Selector for rating value.
  68. *
  69. * @var string
  70. */
  71. protected $ratingValue = '.rating';
  72. /**
  73. * Click add review link.
  74. *
  75. * @return void
  76. */
  77. public function clickAddReviewLink()
  78. {
  79. $this->getAddReviewLink()->click();
  80. }
  81. /**
  82. * Is visible review items.
  83. *
  84. * @return bool
  85. */
  86. public function isVisibleReviewItems()
  87. {
  88. return $this->_rootElement->find($this->itemsSelector)->isVisible();
  89. }
  90. /**
  91. * Get add review link.
  92. *
  93. * @return ElementInterface
  94. */
  95. public function getAddReviewLink()
  96. {
  97. return $this->_rootElement->find($this->addReviewLink);
  98. }
  99. /**
  100. * Get all reviews.
  101. *
  102. * @return array
  103. */
  104. public function getItems()
  105. {
  106. $items = [];
  107. if (!$this->_rootElement->find($this->itemsSelector)->isVisible()) {
  108. return [];
  109. }
  110. $reviewsTitles = $this->_rootElement->getElements($this->itemsSelector);
  111. foreach ($reviewsTitles as $title) {
  112. $reviewTitle = $this->getReviewTitle($title);;
  113. $items[] = [
  114. 'title' => $reviewTitle,
  115. 'detail' => $this->getReviewText($reviewTitle),
  116. 'nickname' => $this->getReviewAuthor($reviewTitle),
  117. 'ratings' => $this->getReviewRatings($reviewTitle)
  118. ];
  119. }
  120. return $items;
  121. }
  122. /**
  123. * Get review ratings.
  124. *
  125. * @param string $reviewTitle
  126. * @return array|null
  127. */
  128. protected function getReviewRatings($reviewTitle)
  129. {
  130. $ratings = [];
  131. $ratingsSelector = $this->getRatingsSelector($reviewTitle);
  132. if (!$this->_rootElement->find($ratingsSelector, Locator::SELECTOR_XPATH)->isVisible()) {
  133. return null;
  134. }
  135. $ratingsElements = $this->_rootElement->getElements($ratingsSelector, Locator::SELECTOR_XPATH);
  136. foreach ($ratingsElements as $itemRating) {
  137. $ratings[] = [
  138. 'title' => strtolower($itemRating->getText()),
  139. 'rating' => $this->getRatingValue($itemRating)
  140. ];
  141. }
  142. return $ratings;
  143. }
  144. /**
  145. * Get ratings selector.
  146. *
  147. * @param string $reviewTitle
  148. * @return string
  149. */
  150. protected function getRatingsSelector($reviewTitle)
  151. {
  152. return $this->getReviewSelector($reviewTitle, 'Ratings');
  153. }
  154. /**
  155. * Get rating value.
  156. *
  157. * @param ElementInterface $itemRating
  158. * @return string
  159. */
  160. protected function getRatingValue(ElementInterface $itemRating)
  161. {
  162. $ratingValue = $itemRating->find($this->ratingValue)->getAttribute('style');
  163. preg_match('`(\d+)%`', $ratingValue, $matches);
  164. return isset($matches[1]) ? $matches[1] / 20 : null;
  165. }
  166. /**
  167. * Get review's title.
  168. *
  169. * @param ElementInterface $titleElement
  170. * @return string
  171. */
  172. protected function getReviewTitle(ElementInterface $titleElement)
  173. {
  174. return strtolower($titleElement->getText());
  175. }
  176. /**
  177. * Get review's author.
  178. *
  179. * @param string $reviewTitle
  180. * @return string
  181. */
  182. protected function getReviewAuthor($reviewTitle)
  183. {
  184. $reviewAuthor = $this->_rootElement->find($this->getReviewAuthorSelector($reviewTitle), Locator::SELECTOR_XPATH)
  185. ->getText();
  186. return strtolower(trim(str_replace('REVIEW BY', '', explode('/', $reviewAuthor)[0])));
  187. }
  188. /**
  189. * Get review's text.
  190. *
  191. * @param string $reviewTitle
  192. * @return string
  193. */
  194. protected function getReviewText($reviewTitle)
  195. {
  196. $reviewText = $this->_rootElement->find($this->getReviewTextSelector($reviewTitle), Locator::SELECTOR_XPATH)
  197. ->getText();
  198. return explode("\n", $reviewText)[0];
  199. }
  200. /**
  201. * Get review text selector.
  202. *
  203. * @param string $reviewTitle
  204. * @return string
  205. */
  206. protected function getReviewTextSelector($reviewTitle)
  207. {
  208. return $this->getReviewSelector($reviewTitle, 'Text');
  209. }
  210. /**
  211. * Get review author selector.
  212. *
  213. * @param string $reviewTitle
  214. * @return string
  215. */
  216. protected function getReviewAuthorSelector($reviewTitle)
  217. {
  218. return $this->getReviewSelector($reviewTitle, 'Author');
  219. }
  220. /**
  221. * Get review entity selector.
  222. *
  223. * @param string $reviewTitle
  224. * @param string $type
  225. * @return string
  226. */
  227. protected function getReviewSelector($reviewTitle, $type)
  228. {
  229. $property = 'item' . $type;
  230. $specifySelector = property_exists($this, $property) ? $this->$property : '';
  231. return sprintf($this->itemRowSelector . $specifySelector, $reviewTitle);
  232. }
  233. }