Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigTest.php
Go to the documentation of this file.
1 <?php
7 
11 class ConfigTest extends \PHPUnit\Framework\TestCase
12 {
16  protected $_model;
17 
21  protected $_eventManagerMock;
22 
27 
31  protected $_transFactoryMock;
32 
36  protected $_appConfigMock;
37 
41  protected $_applicationMock;
42 
46  protected $_configLoaderMock;
47 
51  protected $_dataFactoryMock;
52 
56  protected $_storeManager;
57 
61  protected $_configStructure;
62 
66  private $_settingsChecker;
67 
68  protected function setUp()
69  {
70  $this->_eventManagerMock = $this->createMock(\Magento\Framework\Event\ManagerInterface::class);
71  $this->_structureReaderMock = $this->createPartialMock(
72  \Magento\Config\Model\Config\Structure\Reader::class,
73  ['getConfiguration']
74  );
75  $this->_configStructure = $this->createMock(\Magento\Config\Model\Config\Structure::class);
76 
77  $this->_structureReaderMock->expects(
78  $this->any()
79  )->method(
80  'getConfiguration'
81  )->will(
82  $this->returnValue($this->_configStructure)
83  );
84 
85  $this->_transFactoryMock = $this->createPartialMock(
86  \Magento\Framework\DB\TransactionFactory::class,
87  ['create', 'addObject']
88  );
89  $this->_appConfigMock = $this->createMock(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
90  $this->_configLoaderMock = $this->createPartialMock(
91  \Magento\Config\Model\Config\Loader::class,
92  ['getConfigByPath']
93  );
94  $this->_dataFactoryMock = $this->createMock(\Magento\Framework\App\Config\ValueFactory::class);
95 
96  $this->_storeManager = $this->getMockForAbstractClass(\Magento\Store\Model\StoreManagerInterface::class);
97 
98  $this->_settingsChecker = $this
99  ->createMock(\Magento\Config\Model\Config\Reader\Source\Deployed\SettingChecker::class);
100 
101  $this->_model = new \Magento\Config\Model\Config(
102  $this->_appConfigMock,
103  $this->_eventManagerMock,
104  $this->_configStructure,
105  $this->_transFactoryMock,
106  $this->_configLoaderMock,
107  $this->_dataFactoryMock,
108  $this->_storeManager,
109  $this->_settingsChecker
110  );
111  }
112 
114  {
115  $this->_configLoaderMock->expects($this->never())->method('getConfigByPath');
116  $this->_model->save();
117  }
118 
120  {
121  $this->_structureReaderMock->expects($this->never())->method('getConfiguration');
122  $this->assertNull($this->_model->getSection());
123  $this->assertNull($this->_model->getWebsite());
124  $this->assertNull($this->_model->getStore());
125  $this->_model->save();
126  $this->assertSame('', $this->_model->getSection());
127  $this->assertSame('', $this->_model->getWebsite());
128  $this->assertSame('', $this->_model->getStore());
129  }
130 
132  {
133  $transactionMock = $this->createMock(\Magento\Framework\DB\Transaction::class);
134 
135  $this->_transFactoryMock->expects($this->any())->method('create')->will($this->returnValue($transactionMock));
136 
137  $this->_configLoaderMock->expects($this->any())->method('getConfigByPath')->will($this->returnValue([]));
138 
139  $this->_eventManagerMock->expects(
140  $this->at(0)
141  )->method(
142  'dispatch'
143  )->with(
144  $this->equalTo('admin_system_config_changed_section_'),
145  $this->arrayHasKey('website')
146  );
147 
148  $this->_eventManagerMock->expects(
149  $this->at(0)
150  )->method(
151  'dispatch'
152  )->with(
153  $this->equalTo('admin_system_config_changed_section_'),
154  $this->arrayHasKey('store')
155  );
156 
157  $this->_model->setGroups(['1' => ['data']]);
158  $this->_model->save();
159  }
160 
161  public function testDoNotSaveReadOnlyFields()
162  {
163  $transactionMock = $this->createMock(\Magento\Framework\DB\Transaction::class);
164  $this->_transFactoryMock->expects($this->any())->method('create')->will($this->returnValue($transactionMock));
165 
166  $this->_settingsChecker->expects($this->any())->method('isReadOnly')->will($this->returnValue(true));
167  $this->_configLoaderMock->expects($this->any())->method('getConfigByPath')->will($this->returnValue([]));
168 
169  $this->_model->setGroups(['1' => ['fields' => ['key' => ['data']]]]);
170  $this->_model->setSection('section');
171 
172  $group = $this->createMock(\Magento\Config\Model\Config\Structure\Element\Group::class);
173  $group->method('getPath')->willReturn('section/1');
174 
175  $field = $this->createMock(\Magento\Config\Model\Config\Structure\Element\Field::class);
176  $field->method('getGroupPath')->willReturn('section/1');
177  $field->method('getId')->willReturn('key');
178 
179  $this->_configStructure->expects($this->at(0))
180  ->method('getElement')
181  ->with('section/1')
182  ->will($this->returnValue($group));
183  $this->_configStructure->expects($this->at(1))
184  ->method('getElement')
185  ->with('section/1')
186  ->will($this->returnValue($group));
187  $this->_configStructure->expects($this->at(2))
188  ->method('getElement')
189  ->with('section/1/key')
190  ->will($this->returnValue($field));
191 
192  $backendModel = $this->createPartialMock(
193  \Magento\Framework\App\Config\Value::class,
194  ['addData']
195  );
196  $this->_dataFactoryMock->expects($this->any())->method('create')->will($this->returnValue($backendModel));
197 
198  $this->_transFactoryMock->expects($this->never())->method('addObject');
199  $backendModel->expects($this->never())->method('addData');
200 
201  $this->_model->save();
202  }
203 
204  public function testSaveToCheckScopeDataSet()
205  {
206  $transactionMock = $this->createMock(\Magento\Framework\DB\Transaction::class);
207  $this->_transFactoryMock->expects($this->any())->method('create')->will($this->returnValue($transactionMock));
208 
209  $this->_configLoaderMock->expects($this->any())->method('getConfigByPath')->will($this->returnValue([]));
210 
211  $this->_eventManagerMock->expects($this->at(0))
212  ->method('dispatch')
213  ->with(
214  $this->equalTo('admin_system_config_changed_section_section'),
215  $this->arrayHasKey('website')
216  );
217  $this->_eventManagerMock->expects($this->at(0))
218  ->method('dispatch')
219  ->with(
220  $this->equalTo('admin_system_config_changed_section_section'),
221  $this->arrayHasKey('store')
222  );
223 
224  $group = $this->createMock(\Magento\Config\Model\Config\Structure\Element\Group::class);
225  $group->method('getPath')->willReturn('section/1');
226 
227  $field = $this->createMock(\Magento\Config\Model\Config\Structure\Element\Field::class);
228  $field->method('getGroupPath')->willReturn('section/1');
229  $field->method('getId')->willReturn('key');
230 
231  $this->_configStructure->expects($this->at(0))
232  ->method('getElement')
233  ->with('section/1')
234  ->will($this->returnValue($group));
235  $this->_configStructure->expects($this->at(1))
236  ->method('getElement')
237  ->with('section/1')
238  ->will($this->returnValue($group));
239  $this->_configStructure->expects($this->at(2))
240  ->method('getElement')
241  ->with('section/1/key')
242  ->will($this->returnValue($field));
243  $this->_configStructure->expects($this->at(3))
244  ->method('getElement')
245  ->with('section/1')
246  ->will($this->returnValue($group));
247  $this->_configStructure->expects($this->at(4))
248  ->method('getElement')
249  ->with('section/1/key')
250  ->will($this->returnValue($field));
251 
252  $website = $this->createMock(\Magento\Store\Model\Website::class);
253  $website->expects($this->any())->method('getCode')->will($this->returnValue('website_code'));
254  $this->_storeManager->expects($this->any())->method('getWebsite')->will($this->returnValue($website));
255  $this->_storeManager->expects($this->any())->method('getWebsites')->will($this->returnValue([$website]));
256  $this->_storeManager->expects($this->any())->method('isSingleStoreMode')->will($this->returnValue(true));
257 
258  $this->_model->setWebsite('website');
259  $this->_model->setSection('section');
260  $this->_model->setGroups(['1' => ['fields' => ['key' => ['data']]]]);
261 
262  $backendModel = $this->createPartialMock(
263  \Magento\Framework\App\Config\Value::class,
264  ['setPath', 'addData', '__sleep', '__wakeup']
265  );
266  $backendModel->expects($this->once())
267  ->method('addData')
268  ->with([
269  'field' => 'key',
270  'groups' => [1 => ['fields' => ['key' => ['data']]]],
271  'group_id' => null,
272  'scope' => 'websites',
273  'scope_id' => 0,
274  'scope_code' => 'website_code',
275  'field_config' => null,
276  'fieldset_data' => ['key' => null],
277  ]);
278  $backendModel->expects($this->once())
279  ->method('setPath')
280  ->with('section/1/key')
281  ->will($this->returnValue($backendModel));
282 
283  $this->_dataFactoryMock->expects($this->any())->method('create')->will($this->returnValue($backendModel));
284 
285  $this->_model->save();
286  }
287 
288  public function testSetDataByPath()
289  {
290  $value = 'value';
291  $path = '<section>/<group>/<field>';
292  $this->_model->setDataByPath($path, $value);
293  $expected = [
294  'section' => '<section>',
295  'groups' => [
296  '<group>' => [
297  'fields' => [
298  '<field>' => ['value' => $value],
299  ],
300  ],
301  ],
302  ];
303  $this->assertSame($expected, $this->_model->getData());
304  }
305 
310  public function testSetDataByPathEmpty()
311  {
312  $this->_model->setDataByPath('', 'value');
313  }
314 
321  public function testSetDataByPathWrongDepth($path, $expectedException)
322  {
323  $expectedException = 'Allowed depth of configuration is 3 (<section>/<group>/<field>). ' . $expectedException;
324  $this->expectException('\UnexpectedValueException');
325  $this->expectExceptionMessage($expectedException);
326  $value = 'value';
327  $this->_model->setDataByPath($path, $value);
328  }
329 
334  {
335  return [
336  'depth 2' => ['section/group', "Your configuration depth is 2 for path 'section/group'"],
337  'depth 1' => ['section', "Your configuration depth is 1 for path 'section'"],
338  'depth 4' => ['section/group/field/sub-field', "Your configuration depth is 4 for path"
339  . " 'section/group/field/sub-field'", ],
340  ];
341  }
342 }
$group
Definition: sections.phtml:16
$value
Definition: gender.phtml:16
testSetDataByPathWrongDepth($path, $expectedException)
Definition: ConfigTest.php:321