VariableTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. <?php
  2. /***
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Variable\Test\Unit\Model;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. use Magento\Variable\Model\ResourceModel\Variable\Collection;
  9. class VariableTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Variable\Model\Variable
  13. */
  14. private $model;
  15. /**
  16. * @var \Magento\Framework\Escaper|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. private $escaperMock;
  19. /**
  20. * @var \Magento\Variable\Model\ResourceModel\Variable|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. private $resourceMock;
  23. /**
  24. * @var \Magento\Variable\Model\ResourceModel\Variable\Collection|\PHPUnit_Framework_MockObject_MockObject
  25. */
  26. private $resourceCollectionMock;
  27. /**
  28. * @var \Magento\Framework\Phrase
  29. */
  30. private $validationFailedPhrase;
  31. /**
  32. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  33. */
  34. private $objectManager;
  35. protected function setUp()
  36. {
  37. $this->objectManager = new ObjectManager($this);
  38. $this->escaperMock = $this->getMockBuilder(\Magento\Framework\Escaper::class)
  39. ->disableOriginalConstructor()
  40. ->getMock();
  41. $this->resourceMock = $this->getMockBuilder(\Magento\Variable\Model\ResourceModel\Variable::class)
  42. ->disableOriginalConstructor()
  43. ->getMock();
  44. $this->resourceCollectionMock = $this->getMockBuilder(Collection::class)
  45. ->disableOriginalConstructor()
  46. ->getMock();
  47. $this->model = $this->objectManager->getObject(
  48. \Magento\Variable\Model\Variable::class,
  49. [
  50. 'escaper' => $this->escaperMock,
  51. 'resource' => $this->resourceMock,
  52. 'resourceCollection' => $this->resourceCollectionMock,
  53. ]
  54. );
  55. $this->validationFailedPhrase = __('Validation has failed.');
  56. }
  57. public function testGetValueHtml()
  58. {
  59. $type = \Magento\Variable\Model\Variable::TYPE_HTML;
  60. $html = '<html/>';
  61. $this->model->setData('html_value', $html);
  62. $this->assertSame($html, $this->model->getValue($type));
  63. }
  64. public function testGetValueEmptyHtml()
  65. {
  66. $type = \Magento\Variable\Model\Variable::TYPE_HTML;
  67. $html = '';
  68. $plain = 'unescaped_plain_text';
  69. $escapedPlain = 'escaped_plain_text';
  70. $this->model->setData('html_value', $html);
  71. $this->model->setData('plain_value', $plain);
  72. $this->escaperMock->expects($this->once())
  73. ->method('escapeHtml')
  74. ->with($plain)
  75. ->willReturn($escapedPlain);
  76. $this->assertSame($escapedPlain, $this->model->getValue($type));
  77. }
  78. public function testGetValueText()
  79. {
  80. $type = \Magento\Variable\Model\Variable::TYPE_TEXT;
  81. $plain = 'plain';
  82. $this->model->setData('plain_value', $plain);
  83. $this->assertSame($plain, $this->model->getValue($type));
  84. }
  85. /**
  86. * @dataProvider validateMissingInfoDataProvider
  87. */
  88. public function testValidateMissingInfo($code, $name)
  89. {
  90. $this->model->setCode($code)->setName($name);
  91. $this->assertEquals($this->validationFailedPhrase, $this->model->validate());
  92. }
  93. /**
  94. * @dataProvider validateDataProvider
  95. */
  96. public function testValidate($variableArray, $objectId, $expectedResult)
  97. {
  98. $code = 'variable_code';
  99. $this->model->setCode($code)->setName('some_name');
  100. $this->resourceMock->expects($this->once())
  101. ->method('getVariableByCode')
  102. ->with($code)
  103. ->willReturn($variableArray);
  104. $this->model->setId($objectId);
  105. $this->assertEquals($expectedResult, $this->model->validate($variableArray));
  106. }
  107. public function testGetVariablesOptionArrayNoGroup()
  108. {
  109. $origOptions = [
  110. ['value' => 'VAL', 'label' => 'LBL'],
  111. ];
  112. $transformedOptions = [
  113. ['value' => '{{customVar code=VAL}}', 'label' => __('%1', 'LBL')],
  114. ];
  115. $this->resourceCollectionMock->expects($this->any())
  116. ->method('toOptionArray')
  117. ->willReturn($origOptions);
  118. $this->escaperMock->expects($this->once())
  119. ->method('escapeHtml')
  120. ->with($origOptions[0]['label'])
  121. ->willReturn($origOptions[0]['label']);
  122. $this->assertEquals($transformedOptions, $this->model->getVariablesOptionArray());
  123. }
  124. public function testGetVariablesOptionArrayWithGroup()
  125. {
  126. $origOptions = [
  127. ['value' => 'VAL', 'label' => 'LBL'],
  128. ];
  129. $transformedOptions = [
  130. [
  131. 'label' => __('Custom Variables'),
  132. 'value' => [
  133. ['value' => '{{customVar code=VAL}}', 'label' => __('%1', 'LBL')],
  134. ],
  135. ],
  136. ];
  137. $this->resourceCollectionMock->expects($this->any())
  138. ->method('toOptionArray')
  139. ->willReturn($origOptions);
  140. $this->escaperMock->expects($this->atLeastOnce())
  141. ->method('escapeHtml')
  142. ->with($origOptions[0]['label'])
  143. ->willReturn($origOptions[0]['label']);
  144. $this->assertEquals($transformedOptions, $this->model->getVariablesOptionArray(true));
  145. }
  146. /**
  147. * @return array
  148. */
  149. public function validateDataProvider()
  150. {
  151. $variable = [
  152. 'variable_id' => 'matching_id',
  153. ];
  154. return [
  155. 'Empty Variable' => [[], null, true],
  156. 'IDs match' => [$variable, 'matching_id', true],
  157. 'IDs do not match' => [$variable, 'non_matching_id', __('Variable Code must be unique.')],
  158. ];
  159. }
  160. /**
  161. * @return array
  162. */
  163. public function validateMissingInfoDataProvider()
  164. {
  165. return [
  166. 'Missing code' => ['', 'some-name'],
  167. 'Missing name' => ['some-code', ''],
  168. ];
  169. }
  170. }