Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
VariableTest.php
Go to the documentation of this file.
1 <?php
2 /***
3  * Copyright © Magento, Inc. All rights reserved.
4  * See COPYING.txt for license details.
5  */
7 
10 
11 class VariableTest extends \PHPUnit\Framework\TestCase
12 {
16  private $model;
17 
21  private $escaperMock;
22 
26  private $resourceMock;
27 
31  private $resourceCollectionMock;
32 
36  private $validationFailedPhrase;
37 
41  private $objectManager;
42 
43  protected function setUp()
44  {
45  $this->objectManager = new ObjectManager($this);
46  $this->escaperMock = $this->getMockBuilder(\Magento\Framework\Escaper::class)
47  ->disableOriginalConstructor()
48  ->getMock();
49  $this->resourceMock = $this->getMockBuilder(\Magento\Variable\Model\ResourceModel\Variable::class)
50  ->disableOriginalConstructor()
51  ->getMock();
52  $this->resourceCollectionMock = $this->getMockBuilder(Collection::class)
53  ->disableOriginalConstructor()
54  ->getMock();
55  $this->model = $this->objectManager->getObject(
56  \Magento\Variable\Model\Variable::class,
57  [
58  'escaper' => $this->escaperMock,
59  'resource' => $this->resourceMock,
60  'resourceCollection' => $this->resourceCollectionMock,
61  ]
62  );
63  $this->validationFailedPhrase = __('Validation has failed.');
64  }
65 
66  public function testGetValueHtml()
67  {
69  $html = '<html/>';
70  $this->model->setData('html_value', $html);
71  $this->assertSame($html, $this->model->getValue($type));
72  }
73 
74  public function testGetValueEmptyHtml()
75  {
77  $html = '';
78  $plain = 'unescaped_plain_text';
79  $escapedPlain = 'escaped_plain_text';
80  $this->model->setData('html_value', $html);
81  $this->model->setData('plain_value', $plain);
82  $this->escaperMock->expects($this->once())
83  ->method('escapeHtml')
84  ->with($plain)
85  ->willReturn($escapedPlain);
86  $this->assertSame($escapedPlain, $this->model->getValue($type));
87  }
88 
89  public function testGetValueText()
90  {
92  $plain = 'plain';
93  $this->model->setData('plain_value', $plain);
94  $this->assertSame($plain, $this->model->getValue($type));
95  }
96 
101  {
102  $this->model->setCode($code)->setName($name);
103  $this->assertEquals($this->validationFailedPhrase, $this->model->validate());
104  }
105 
109  public function testValidate($variableArray, $objectId, $expectedResult)
110  {
111  $code = 'variable_code';
112  $this->model->setCode($code)->setName('some_name');
113  $this->resourceMock->expects($this->once())
114  ->method('getVariableByCode')
115  ->with($code)
116  ->willReturn($variableArray);
117  $this->model->setId($objectId);
118  $this->assertEquals($expectedResult, $this->model->validate($variableArray));
119  }
120 
122  {
123  $origOptions = [
124  ['value' => 'VAL', 'label' => 'LBL'],
125  ];
126 
127  $transformedOptions = [
128  ['value' => '{{customVar code=VAL}}', 'label' => __('%1', 'LBL')],
129  ];
130 
131  $this->resourceCollectionMock->expects($this->any())
132  ->method('toOptionArray')
133  ->willReturn($origOptions);
134  $this->escaperMock->expects($this->once())
135  ->method('escapeHtml')
136  ->with($origOptions[0]['label'])
137  ->willReturn($origOptions[0]['label']);
138  $this->assertEquals($transformedOptions, $this->model->getVariablesOptionArray());
139  }
140 
142  {
143  $origOptions = [
144  ['value' => 'VAL', 'label' => 'LBL'],
145  ];
146 
147  $transformedOptions = [
148  [
149  'label' => __('Custom Variables'),
150  'value' => [
151  ['value' => '{{customVar code=VAL}}', 'label' => __('%1', 'LBL')],
152  ],
153  ],
154  ];
155 
156  $this->resourceCollectionMock->expects($this->any())
157  ->method('toOptionArray')
158  ->willReturn($origOptions);
159  $this->escaperMock->expects($this->atLeastOnce())
160  ->method('escapeHtml')
161  ->with($origOptions[0]['label'])
162  ->willReturn($origOptions[0]['label']);
163  $this->assertEquals($transformedOptions, $this->model->getVariablesOptionArray(true));
164  }
165 
169  public function validateDataProvider()
170  {
171  $variable = [
172  'variable_id' => 'matching_id',
173  ];
174  return [
175  'Empty Variable' => [[], null, true],
176  'IDs match' => [$variable, 'matching_id', true],
177  'IDs do not match' => [$variable, 'non_matching_id', __('Variable Code must be unique.')],
178  ];
179  }
180 
185  {
186  return [
187  'Missing code' => ['', 'some-name'],
188  'Missing name' => ['some-code', ''],
189  ];
190  }
191 }
__()
Definition: __.php:13
$variable
Definition: variable.php:7
$type
Definition: item.phtml:13
testValidate($variableArray, $objectId, $expectedResult)
$code
Definition: info.phtml:12
if(!isset($_GET['name'])) $name
Definition: log.php:14