/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Order/View.php

https://gitlab.com/crazybutterfly815/magento2 · PHP · 323 lines · 110 code · 34 blank · 179 comment · 0 complexity · 10da1cc87785f1a97ad8eca8d1e8807c MD5 · raw file

  1. <?php
  2. /**
  3. * Copyright © 2016 Magento. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Sales\Test\Block\Order;
  7. use Magento\Mtf\Block\Block;
  8. use Magento\Mtf\Client\Locator;
  9. /**
  10. * Class View
  11. * View block on order's view page
  12. *
  13. * @SuppressWarnings(PHPMD.TooManyFields)
  14. */
  15. class View extends Block
  16. {
  17. /**
  18. * Item block
  19. *
  20. * @var string
  21. */
  22. protected $itemBlock = '//*[@class="order-title" and contains(.,"%d")]';
  23. /**
  24. * Content block
  25. *
  26. * @var string
  27. */
  28. protected $content = '//following-sibling::div[contains(@class,"order-items")][1]';
  29. /**
  30. * Link xpath selector
  31. *
  32. * @var string
  33. */
  34. protected $link = '//*[contains(@class,"order-links")]//a[normalize-space(.)="%s"]';
  35. /**
  36. * Grand total search mask
  37. *
  38. * @var string
  39. */
  40. protected $grandTotal = '.grand_total span';
  41. /**
  42. * Grand total including tax search mask
  43. *
  44. * @var string
  45. */
  46. protected $grandTotalInclTax = '.grand_total_incl span';
  47. /**
  48. * Subtotal search mask
  49. *
  50. * @var string
  51. */
  52. protected $subtotal = '.subtotal .amount span';
  53. /**
  54. * Subtotal excluding tax search mask
  55. *
  56. * @var string
  57. */
  58. protected $subtotalExclTax = '.subtotal_excl span';
  59. /**
  60. * Subtotal including tax search mask
  61. *
  62. * @var string
  63. */
  64. protected $subtotalInclTax = '.subtotal_incl span';
  65. /**
  66. * Tax search mask
  67. *
  68. * @var string
  69. */
  70. protected $tax = '.totals-tax span';
  71. /**
  72. * Discount search mask
  73. *
  74. * @var string
  75. */
  76. protected $discount = '.discount span';
  77. /**
  78. * Shipping search mask
  79. *
  80. * @var string
  81. */
  82. protected $shippingExclTax = '.shipping span';
  83. /**
  84. * Shipping search mask
  85. *
  86. * @var string
  87. */
  88. protected $shippingInclTax = '.shipping_incl span';
  89. /**
  90. * Product price excluding tax search mask
  91. *
  92. * @var string
  93. */
  94. protected $itemExclTax = '//tr[contains (.,"%s")]/td[@class="col price"]/span[@class="price-excluding-tax"]/span';
  95. /**
  96. * Product price including tax search mask
  97. *
  98. * @var string
  99. */
  100. protected $itemInclTax = '//tr[contains (.,"%s")]/td[@class="col price"]/span[@class="price-including-tax"]/span';
  101. // @codingStandardsIgnoreStart
  102. /**
  103. * Product price subtotal excluding tax search mask
  104. *
  105. * @var string
  106. */
  107. protected $itemSubExclTax = '//tr[contains (.,"%s")]/td[@class="col subtotal"]/span[@class="price-excluding-tax"]/span';
  108. /**
  109. * Product price subtotal including tax search mask
  110. *
  111. * @var string
  112. */
  113. protected $itemSubInclTax = '//tr[contains (.,"%s")]/td[@class="col subtotal"]/span[@class="price-including-tax"]/span';
  114. // @codingStandardsIgnoreEnd
  115. /**
  116. * Get item block
  117. *
  118. * @param int $id [optional]
  119. * @return Items
  120. */
  121. public function getItemBlock($id = null)
  122. {
  123. $selector = ($id === null) ? $this->content : sprintf($this->itemBlock, $id) . $this->content;
  124. return $this->blockFactory->create(
  125. \Magento\Sales\Test\Block\Order\Items::class,
  126. ['element' => $this->_rootElement->find($selector, Locator::SELECTOR_XPATH)]
  127. );
  128. }
  129. /**
  130. * Open link by name
  131. *
  132. * @param string $name
  133. * @return void
  134. */
  135. public function openLinkByName($name)
  136. {
  137. $this->_rootElement->find(sprintf($this->link, $name), Locator::SELECTOR_XPATH)->click();
  138. sleep(3); // TODO: remove after resolving an issue with ajax on Frontend.
  139. }
  140. /**
  141. * Get Grand Total Text
  142. *
  143. * @return string
  144. */
  145. public function getGrandTotal()
  146. {
  147. $grandTotal = $this->_rootElement->find($this->grandTotal, Locator::SELECTOR_CSS)->getText();
  148. return $this->escapeCurrency($grandTotal);
  149. }
  150. /**
  151. * Get Item price excluding tax
  152. *
  153. * @param string $productName
  154. * @return string|null
  155. */
  156. public function getItemPriceExclTax($productName)
  157. {
  158. $locator = sprintf($this->itemExclTax, $productName);
  159. $price = $this->_rootElement->find($locator, Locator::SELECTOR_XPATH);
  160. return $price->isVisible() ? $this->escapeCurrency($price->getText()) : null;
  161. }
  162. /**
  163. * Get Item price excluding tax
  164. *
  165. * @param string $productName
  166. * @return string|null
  167. */
  168. public function getItemPriceInclTax($productName)
  169. {
  170. $locator = sprintf($this->itemInclTax, $productName);
  171. $price = $this->_rootElement->find($locator, Locator::SELECTOR_XPATH);
  172. return $price->isVisible() ? $this->escapeCurrency($price->getText()) : null;
  173. }
  174. /**
  175. * Get Item price excluding tax
  176. *
  177. * @param string $productName
  178. * @return string|null
  179. */
  180. public function getItemSubExclTax($productName)
  181. {
  182. $locator = sprintf($this->itemSubExclTax, $productName);
  183. $price = $this->_rootElement->find($locator, Locator::SELECTOR_XPATH);
  184. return $price->isVisible() ? $this->escapeCurrency($price->getText()) : null;
  185. }
  186. /**
  187. * Get Item price excluding tax
  188. *
  189. * @param string $productName
  190. * @return string|null
  191. */
  192. public function getItemSubInclTax($productName)
  193. {
  194. $locator = sprintf($this->itemSubInclTax, $productName);
  195. $price = $this->_rootElement->find($locator, Locator::SELECTOR_XPATH);
  196. return $price->isVisible() ? $this->escapeCurrency($price->getText()) : null;
  197. }
  198. /**
  199. * Get Grand Total Text
  200. *
  201. * @return string|null
  202. */
  203. public function getGrandTotalInclTax()
  204. {
  205. $grandTotal = $this->_rootElement->find($this->grandTotalInclTax, Locator::SELECTOR_CSS)->getText();
  206. return $this->escapeCurrency($grandTotal);
  207. }
  208. /**
  209. * Get Tax text from Order Totals
  210. *
  211. * @return string
  212. */
  213. public function getTax()
  214. {
  215. $tax = $this->_rootElement->find($this->tax, Locator::SELECTOR_CSS)->getText();
  216. return $this->escapeCurrency($tax);
  217. }
  218. /**
  219. * Get Tax text from Order Totals
  220. *
  221. * @return string|null
  222. */
  223. public function getDiscount()
  224. {
  225. $discount = $this->_rootElement->find($this->discount, Locator::SELECTOR_CSS);
  226. return $discount->isVisible() ? $this->escapeCurrency($discount->getText()) : null;
  227. }
  228. /**
  229. * Get Subtotal text
  230. *
  231. * @return string
  232. */
  233. public function getSubtotal()
  234. {
  235. $subTotal = $this->_rootElement->find($this->subtotal, Locator::SELECTOR_CSS)->getText();
  236. return $this->escapeCurrency($subTotal);
  237. }
  238. /**
  239. * Get Subtotal text
  240. *
  241. * @return string
  242. */
  243. public function getSubtotalExclTax()
  244. {
  245. $subTotal = $this->_rootElement->find($this->subtotalExclTax, Locator::SELECTOR_CSS)->getText();
  246. return $this->escapeCurrency($subTotal);
  247. }
  248. /**
  249. * Get Subtotal text
  250. *
  251. * @return string
  252. */
  253. public function getSubtotalInclTax()
  254. {
  255. $subTotal = $this->_rootElement->find($this->subtotalInclTax, Locator::SELECTOR_CSS)->getText();
  256. return $this->escapeCurrency($subTotal);
  257. }
  258. /**
  259. * Get Shipping Excluding tax price text
  260. *
  261. * @return string|null
  262. */
  263. public function getShippingInclTax()
  264. {
  265. $subTotal = $this->_rootElement->find($this->shippingInclTax, Locator::SELECTOR_CSS);
  266. return $subTotal->isVisible() ? $this->escapeCurrency($subTotal->getText()) : null;
  267. }
  268. /**
  269. * Get Shipping Including tax price text
  270. *
  271. * @return string|null
  272. */
  273. public function getShippingExclTax()
  274. {
  275. $subTotal = $this->_rootElement->find($this->shippingExclTax, Locator::SELECTOR_CSS);
  276. return $subTotal->isVisible() ? $this->escapeCurrency($subTotal->getText()) : null;
  277. }
  278. /**
  279. * Method that escapes currency symbols
  280. *
  281. * @param string $price
  282. * @return string|null
  283. */
  284. protected function escapeCurrency($price)
  285. {
  286. preg_match("/^\\D*\\s*([\\d,\\.]+)\\s*\\D*$/", $price, $matches);
  287. return (isset($matches[1])) ? $matches[1] : null;
  288. }
  289. }