Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
SortbyTest.php
Go to the documentation of this file.
1 <?php
8 
9 class SortbyTest extends \PHPUnit\Framework\TestCase
10 {
11  const DEFAULT_ATTRIBUTE_CODE = 'attribute_name';
12 
16  protected $_model;
17 
21  protected $_objectHelper;
22 
26  protected $_attribute;
27 
31  protected $_scopeConfig;
32 
33  protected function setUp()
34  {
35  $this->markTestSkipped('Due to MAGETWO-48956');
36  $this->_objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
37  $this->_scopeConfig = $this->createMock(\Magento\Framework\App\Config\ScopeConfigInterface::class);
38  $this->_model = $this->_objectHelper->getObject(
39  \Magento\Catalog\Model\Category\Attribute\Backend\Sortby::class,
40  ['scopeConfig' => $this->_scopeConfig]
41  );
42  $this->_attribute = $this->createPartialMock(
43  \Magento\Eav\Model\Entity\Attribute\AbstractAttribute::class,
44  [
45  'getName',
46  '__call',
47  'isValueEmpty',
48  'getEntity',
49  'getFrontend',
50  '__wakeup',
51  'getIsRequired',
52  'getIsUnique'
53  ]
54  );
55 
56  $this->_model->setAttribute($this->_attribute);
57  }
58 
65  public function testBeforeSave($attributeCode, $data, $expected)
66  {
67  $this->_attribute->expects($this->any())->method('getName')->will($this->returnValue($attributeCode));
68  $object = new \Magento\Framework\DataObject($data);
69  $this->_model->beforeSave($object);
70  $this->assertTrue($object->hasData($attributeCode));
71  $this->assertSame($expected, $object->getData($attributeCode));
72  }
73 
77  public function beforeSaveDataProvider()
78  {
79  return [
80  'attribute with specified value' => [
82  [self::DEFAULT_ATTRIBUTE_CODE => 'test_value'],
83  'test_value',
84  ],
85  'attribute with default value' => [
87  [self::DEFAULT_ATTRIBUTE_CODE => null],
88  null,
89  ],
90  'attribute does not exist' => [
92  [],
93  null,
94  ],
95  'attribute sort by empty' => [
96  'available_sort_by',
97  ['available_sort_by' => null],
98  null,
99  ],
100  'attribute sort by' => [
101  'available_sort_by',
102  ['available_sort_by' => ['test', 'value']],
103  'test,value',
104  ]
105  ];
106  }
107 
114  public function testAfterLoad($attributeCode, $data, $expected)
115  {
116  $this->_attribute->expects($this->any())->method('getName')->will($this->returnValue($attributeCode));
117  $object = new \Magento\Framework\DataObject($data);
118  $this->_model->afterLoad($object);
119  $this->assertTrue($object->hasData($attributeCode));
120  $this->assertSame($expected, $object->getData($attributeCode));
121  }
122 
126  public function afterLoadDataProvider()
127  {
128  return [
129  'attribute with specified value' => [
131  [self::DEFAULT_ATTRIBUTE_CODE => 'test_value'],
132  'test_value',
133  ],
134  'attribute sort by empty' => [
135  'available_sort_by',
136  ['available_sort_by' => null],
137  null,
138  ],
139  'attribute sort by' => [
140  'available_sort_by',
141  ['available_sort_by' => 'test,value'],
142  ['test', 'value'],
143  ]
144  ];
145  }
146 
153  public function testValidate($attributeData, $data, $expected)
154  {
155  $this->_attribute->expects($this->any())->method('getName')->will($this->returnValue($attributeData['code']));
156  $this->_attribute
157  ->expects($this->at(1))
158  ->method('getIsRequired')
159  ->will($this->returnValue($attributeData['isRequired']));
160  $this->_attribute
161  ->expects($this->any())
162  ->method('isValueEmpty')
163  ->will($this->returnValue($attributeData['isValueEmpty']));
164  $object = new \Magento\Framework\DataObject($data);
165  $this->assertSame($expected, $this->_model->validate($object));
166  }
167 
171  public function validateDataProvider()
172  {
173  return [
174  'is not required' => [
175  ['code' => self::DEFAULT_ATTRIBUTE_CODE, 'isRequired' => false, 'isValueEmpty' => false],
176  [],
177  true,
178  ],
179  'required, empty, not use config case 1' => [
180  ['code' => self::DEFAULT_ATTRIBUTE_CODE, 'isRequired' => true, 'isValueEmpty' => true],
181  [self::DEFAULT_ATTRIBUTE_CODE => [], 'use_post_data_config' => []],
182  false,
183  ],
184  'required, empty, not use config case 2' => [
185  ['code' => self::DEFAULT_ATTRIBUTE_CODE, 'isRequired' => true, 'isValueEmpty' => true],
186  [self::DEFAULT_ATTRIBUTE_CODE => [], 'use_post_data_config' => ['config']],
187  false,
188  ],
189  'required, empty, use config' => [
190  ['code' => self::DEFAULT_ATTRIBUTE_CODE, 'isRequired' => true, 'isValueEmpty' => true],
191  [self::DEFAULT_ATTRIBUTE_CODE => [], 'use_post_data_config' => [self::DEFAULT_ATTRIBUTE_CODE]],
192  true,
193  ],
194  ];
195  }
196 
197  public function testValidateUnique()
198  {
199  $this->_attribute->expects($this->any())->method('getName')->will($this->returnValue('attribute_name'));
200  $this->_attribute->expects($this->at(1))->method('getIsRequired');
201  $this->_attribute->expects($this->at(2))->method('getIsUnique')->will($this->returnValue(true));
202 
203  $entityMock = $this->getMockForAbstractClass(
204  \Magento\Eav\Model\Entity\AbstractEntity::class,
205  [],
206  '',
207  false,
208  true,
209  true,
210  ['checkAttributeUniqueValue']
211  );
212  $this->_attribute->expects($this->any())->method('getEntity')->will($this->returnValue($entityMock));
213  $entityMock->expects($this->at(0))->method('checkAttributeUniqueValue')->will($this->returnValue(true));
214  $this->assertTrue($this->_model->validate(new \Magento\Framework\DataObject()));
215  }
216 
220  public function testValidateUniqueException()
221  {
222  $this->_attribute->expects($this->any())->method('getName')->will($this->returnValue('attribute_name'));
223  $this->_attribute->expects($this->at(1))->method('getIsRequired');
224  $this->_attribute->expects($this->at(2))->method('getIsUnique')->will($this->returnValue(true));
225 
226  $entityMock = $this->getMockForAbstractClass(
227  \Magento\Eav\Model\Entity\AbstractEntity::class,
228  [],
229  '',
230  false,
231  true,
232  true,
233  ['checkAttributeUniqueValue']
234  );
235  $frontMock = $this->getMockForAbstractClass(
236  \Magento\Eav\Model\Entity\Attribute\Frontend\AbstractFrontend::class,
237  [],
238  '',
239  false,
240  true,
241  true,
242  ['getLabel']
243  );
244  $this->_attribute->expects($this->any())->method('getEntity')->will($this->returnValue($entityMock));
245  $this->_attribute->expects($this->any())->method('getFrontend')->will($this->returnValue($frontMock));
246  $entityMock->expects($this->at(0))->method('checkAttributeUniqueValue')->will($this->returnValue(false));
247  $this->assertTrue($this->_model->validate(new \Magento\Framework\DataObject()));
248  }
249 
256  {
257  $this->_attribute->expects($this->any())->method('getName')->will($this->returnValue($attributeCode));
258  $this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue('value2'));
259  $object = new \Magento\Framework\DataObject($data);
260  $this->assertTrue($this->_model->validate($object));
261  }
262 
267  {
268  return [
269  [
270  'default_sort_by',
271  [
272  'available_sort_by' => ['value1', 'value2'],
273  'default_sort_by' => 'value2',
274  'use_post_data_config' => []
275  ],
276  ],
277  [
278  'default_sort_by',
279  [
280  'available_sort_by' => 'value1,value2',
281  'use_post_data_config' => ['default_sort_by']
282  ]
283  ],
284  [
285  'default_sort_by',
286  [
287  'available_sort_by' => null,
288  'default_sort_by' => null,
289  'use_post_data_config' => ['available_sort_by', 'default_sort_by', 'filter_price_range']
290  ]
291  ],
292  ];
293  }
294 
302  {
303  $this->_attribute->expects($this->any())->method('getName')->will($this->returnValue($attributeCode));
304  $this->_scopeConfig->expects($this->any())->method('getValue')->will($this->returnValue('another value'));
305  $object = new \Magento\Framework\DataObject($data);
306  $this->_model->validate($object);
307  }
308 
313  {
314  return [
315  [
316  'default_sort_by',
317  [
318  'available_sort_by' => null,
319  'use_post_data_config' => ['default_sort_by']
320  ],
321  ],
322  [
323  'default_sort_by',
324  [
325  'available_sort_by' => null,
326  'use_post_data_config' => []
327  ]
328  ],
329  [
330  'default_sort_by',
331  [
332  'available_sort_by' => ['value1', 'value2'],
333  'default_sort_by' => 'another value',
334  'use_post_data_config' => []
335  ]
336  ],
337  [
338  'default_sort_by',
339  [
340  'available_sort_by' => 'value1',
341  'use_post_data_config' => []
342  ]
343  ],
344  ];
345  }
346 }
$attributeCode
Definition: extend.phtml:12