Skip to content

Commit 457b1eb

Browse files
author
Sergey Semenov
committed
Merge remote-tracking branch 'origin/MAGETWO-36041' into BUGS
2 parents a25c8d6 + 4cae1e0 commit 457b1eb

File tree

4 files changed

+252
-0
lines changed

4 files changed

+252
-0
lines changed

app/code/Magento/Backend/Block/Widget/Form.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ public function setForm(\Magento\Framework\Data\Form $form)
111111
$this->_form = $form;
112112
$this->_form->setParent($this);
113113
$this->_form->setBaseUrl($this->_urlBuilder->getBaseUrl());
114+
115+
$customAttributes = $this->getData('custom_attributes');
116+
if (is_array($customAttributes)) {
117+
foreach ($customAttributes as $key => $value) {
118+
$this->_form->addCustomAttribute($key, $value);
119+
}
120+
}
114121
return $this;
115122
}
116123

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Backend\Test\Unit\Block\Widget;
7+
8+
use Magento\Backend\Block\Template\Context;
9+
use Magento\Backend\Block\Widget\Form;
10+
use Magento\Framework\Data\Form as DataForm;
11+
use Magento\Framework\UrlInterface;
12+
13+
class FormTest extends \PHPUnit_Framework_TestCase
14+
{
15+
/** @var Form */
16+
protected $model;
17+
18+
/** @var Context |\PHPUnit_Framework_MockObject_MockObject */
19+
protected $context;
20+
21+
/** @var DataForm |\PHPUnit_Framework_MockObject_MockObject */
22+
protected $dataForm;
23+
24+
/** @var UrlInterface |\PHPUnit_Framework_MockObject_MockObject */
25+
protected $urlBuilder;
26+
27+
protected function setUp()
28+
{
29+
$this->prepareContext();
30+
31+
$this->dataForm = $this->getMockBuilder('Magento\Framework\Data\Form')
32+
->disableOriginalConstructor()
33+
->setMethods([
34+
'setParent',
35+
'setBaseUrl',
36+
'addCustomAttribute',
37+
])
38+
->getMock();
39+
40+
$this->model = new Form(
41+
$this->context
42+
);
43+
}
44+
45+
protected function prepareContext()
46+
{
47+
$this->urlBuilder = $this->getMockBuilder('Magento\Framework\UrlInterface')
48+
->getMock();
49+
50+
$this->context = $this->getMockBuilder('Magento\Backend\Block\Template\Context')
51+
->disableOriginalConstructor()
52+
->getMock();
53+
$this->context->expects($this->any())
54+
->method('getUrlBuilder')
55+
->willReturn($this->urlBuilder);
56+
}
57+
58+
public function testSetForm()
59+
{
60+
$baseUrl = 'base_url';
61+
$attributeKey = 'attribute_key';
62+
$attributeValue = 'attribute_value';
63+
64+
$this->dataForm->expects($this->once())
65+
->method('setParent')
66+
->with($this->model)
67+
->willReturnSelf();
68+
$this->dataForm->expects($this->once())
69+
->method('setBaseUrl')
70+
->with($baseUrl)
71+
->willReturnSelf();
72+
$this->dataForm->expects($this->once())
73+
->method('addCustomAttribute')
74+
->with($attributeKey, $attributeValue)
75+
->willReturnSelf();
76+
77+
$this->urlBuilder->expects($this->once())
78+
->method('getBaseUrl')
79+
->willReturn($baseUrl);
80+
81+
$this->model->setData('custom_attributes', [$attributeKey => $attributeValue]);
82+
$this->assertEquals($this->model, $this->model->setForm($this->dataForm));
83+
}
84+
85+
public function testSetFormNoCustomAttributes()
86+
{
87+
$baseUrl = 'base_url';
88+
89+
$this->dataForm->expects($this->once())
90+
->method('setParent')
91+
->with($this->model)
92+
->willReturnSelf();
93+
$this->dataForm->expects($this->once())
94+
->method('setBaseUrl')
95+
->with($baseUrl)
96+
->willReturnSelf();
97+
98+
$this->urlBuilder->expects($this->once())
99+
->method('getBaseUrl')
100+
->willReturn($baseUrl);
101+
102+
$this->assertEquals($this->model, $this->model->setForm($this->dataForm));
103+
}
104+
}

lib/internal/Magento/Framework/Data/Form/AbstractForm.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ class AbstractForm extends \Magento\Framework\Object
4343
*/
4444
protected $_factoryCollection;
4545

46+
/**
47+
* @var array
48+
*/
49+
protected $customAttributes = [];
50+
4651
/**
4752
* @param Factory $factoryElement
4853
* @param CollectionFactory $factoryCollection
@@ -203,6 +208,7 @@ public function addColumn($elementId, $config)
203208
*
204209
* @param array $arrAttributes
205210
* @return array
211+
* @SuppressWarnings(PHPMD.UnusedFormalParameter)
206212
*/
207213
public function convertToArray(array $arrAttributes = [])
208214
{
@@ -214,4 +220,49 @@ public function convertToArray(array $arrAttributes = [])
214220
}
215221
return $res;
216222
}
223+
224+
/**
225+
* Add custom attribute
226+
*
227+
* @param string $key
228+
* @param mixed $value
229+
* @return $this
230+
*/
231+
public function addCustomAttribute($key, $value)
232+
{
233+
$this->customAttributes[$key] = $value;
234+
return $this;
235+
}
236+
237+
/**
238+
* Convert data into string with defined keys and values
239+
*
240+
* @param array $keys
241+
* @param string $valueSeparator
242+
* @param string $fieldSeparator
243+
* @param string $quote
244+
* @return string
245+
*/
246+
public function serialize($keys = [], $valueSeparator = '=', $fieldSeparator = ' ', $quote = '"')
247+
{
248+
$data = [];
249+
if (empty($keys)) {
250+
$keys = array_keys($this->_data);
251+
}
252+
253+
$customAttributes = array_filter($this->customAttributes);
254+
$keys = array_merge($keys, array_keys(array_diff($this->customAttributes, $customAttributes)));
255+
256+
foreach ($this->_data as $key => $value) {
257+
if (in_array($key, $keys)) {
258+
$data[] = $key . $valueSeparator . $quote . $value . $quote;
259+
}
260+
}
261+
262+
foreach ($customAttributes as $key => $value) {
263+
$data[] = $key . $valueSeparator . $quote . $value . $quote;
264+
}
265+
266+
return implode($fieldSeparator, $data);
267+
}
217268
}

lib/internal/Magento/Framework/Data/Test/Unit/Form/AbstractFormTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,4 +127,94 @@ public function testAddColumn()
127127
$this->allElementsMock->expects($this->once())->method('add')->with($this->elementMock, false);
128128
$this->abstractForm->addColumn('hidden', $config);
129129
}
130+
131+
public function testAddCustomAttribute()
132+
{
133+
$this->assertEquals(
134+
$this->abstractForm,
135+
$this->abstractForm->addCustomAttribute('attribute_key1', 'attribute_value1')
136+
);
137+
138+
$form = clone $this->abstractForm;
139+
$this->assertNotEquals(
140+
$form,
141+
$this->abstractForm->addCustomAttribute('attribute_key2', 'attribute_value2')
142+
);
143+
}
144+
145+
/**
146+
* @param array $keys
147+
* @param array $data
148+
* @param array $customAttributes
149+
* @param string $result
150+
* @dataProvider dataProviderSerialize
151+
*/
152+
public function testSerialize(
153+
$keys,
154+
$data,
155+
$customAttributes,
156+
$result
157+
) {
158+
foreach ($data as $key => $value) {
159+
$this->abstractForm->setData($key, $value);
160+
}
161+
162+
foreach ($customAttributes as $key => $value) {
163+
$this->abstractForm->addCustomAttribute($key, $value);
164+
}
165+
166+
$this->assertEquals($result, $this->abstractForm->serialize($keys));
167+
}
168+
169+
/**
170+
* 1. Keys
171+
* 2. Data
172+
* 3. Custom Attributes
173+
* 4. Result
174+
*
175+
* @return array
176+
*/
177+
public function dataProviderSerialize()
178+
{
179+
return [
180+
[[], [], [], ''],
181+
[['key1'], [], [], ''],
182+
[['key1'], ['key1' => 'value'], [], 'key1="value"'],
183+
[['key1', 'key2'], ['key1' => 'value'], [], 'key1="value"'],
184+
[['key1', 'key2'], ['key1' => 'value', 'key3' => 'value3'], [], 'key1="value"'],
185+
[['key1', 'key2'], ['key1' => 'value', 'key3' => 'value3'], ['custom1' => ''], 'key1="value"'],
186+
[
187+
[
188+
'key1',
189+
'key2',
190+
],
191+
[
192+
'key1' => 'value',
193+
'key3' => 'value3',
194+
],
195+
[
196+
'custom1' => 'custom_value1',
197+
],
198+
'key1="value" custom1="custom_value1"'
199+
],
200+
[
201+
[
202+
'key1',
203+
'key2',
204+
],
205+
[
206+
'key1' => 'value',
207+
'key3' => 'value3',
208+
],
209+
[
210+
'custom1' => 'custom_value1',
211+
'custom2' => '',
212+
'custom3' => 0,
213+
'custom4' => false,
214+
'custom5' => null,
215+
],
216+
'key1="value" custom1="custom_value1"'
217+
],
218+
];
219+
}
130220
}

0 commit comments

Comments
 (0)