/dev/tests/functional/tests/app/Mage/Catalog/Test/Constraint/AssertConfigurableProductPage.php

https://gitlab.com/LisovyiEvhenii/ismextensions · PHP · 142 lines · 61 code · 13 blank · 68 comment · 2 complexity · 1ba0c3f9cb2d0afcc2eba083b77f1775 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\Catalog\Test\Constraint;
  27. /**
  28. * Assert that displayed product data on product page(front-end) equals passed from fixture.
  29. */
  30. class AssertConfigurableProductPage extends AssertProductPage
  31. {
  32. /* tags */
  33. const SEVERITY = 'middle';
  34. /* end tags */
  35. /**
  36. * Verify displayed product data on product page(front-end) equals passed from fixture:
  37. * 1. Product Name
  38. * 2. Price
  39. * 3. SKU
  40. * 4. Description
  41. * 5. Short Description
  42. * 6. Attributes
  43. *
  44. * @return array
  45. */
  46. protected function verify()
  47. {
  48. $errors = parent::verify();
  49. $errors[] = $this->verifyAttributes();
  50. return array_filter($errors);
  51. }
  52. /**
  53. * Verify displayed product attributes on product page(front-end) equals passed from fixture.
  54. *
  55. * @return string|null
  56. */
  57. protected function verifyAttributes()
  58. {
  59. $formOptions = $this->sortOptions($this->productView->getOptions($this->product)['configurable_options']);
  60. $fixtureOptions = $this->prepareFixtureOptions();
  61. $errors = $this->verifyData($fixtureOptions, $formOptions, true, false);
  62. return empty($errors) ? null : $this->prepareErrors($errors, 'Error configurable options:');
  63. }
  64. /**
  65. * Prepare fixture options data.
  66. *
  67. * @return array
  68. */
  69. protected function prepareFixtureOptions()
  70. {
  71. $configurableOptions = [];
  72. $attributesData = $this->product->getConfigurableOptions()['attributes_data'];
  73. $countAttributes = count($attributesData);
  74. for ($i = 0; $i < $countAttributes; $i++) {
  75. $attributeKey = 'attribute_key_' . $i;
  76. $configurableOptions[$attributesData[$attributeKey]['frontend_label']] = [
  77. 'title' => $attributesData[$attributeKey]['frontend_label'],
  78. 'type' => $attributesData[$attributeKey]['frontend_input'],
  79. 'is_require' => 'Yes',
  80. 'options' => $this->getOptionsData($attributesData[$attributeKey]['options'])
  81. ];
  82. }
  83. return $this->sortOptions($configurableOptions);
  84. }
  85. /**
  86. * Get options data.
  87. *
  88. * @param array $options
  89. * @return array
  90. */
  91. protected function getOptionsData(array $options)
  92. {
  93. $optionsData = [];
  94. foreach ($options as $option) {
  95. $optionsData[] = [
  96. 'title' => $option['label'],
  97. 'price' => $this->getOptionPrice($option)
  98. ];
  99. }
  100. return $optionsData;
  101. }
  102. /**
  103. * Options sort.
  104. *
  105. * @param array $options
  106. * @return array
  107. */
  108. protected function sortOptions(array $options)
  109. {
  110. $options = $this->sortDataByPath($options, '::title');
  111. foreach ($options as $key => $option) {
  112. $options[$key] = $this->sortDataByPath($option, 'options::title');
  113. }
  114. return $options;
  115. }
  116. /**
  117. * Get price for option.
  118. *
  119. * @param array $optionData
  120. * @return string
  121. */
  122. protected function getOptionPrice(array $optionData)
  123. {
  124. $price = ('Percentage' == $optionData['price_type'])
  125. ? ($this->product->getPrice() * $optionData['price']) / 100
  126. : $optionData['price'];
  127. return number_format($price, 2);
  128. }
  129. }