DataTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\App\Config;
  7. use Magento\Framework\App\Config;
  8. use Magento\Framework\App\ObjectManager;
  9. class DataTest extends \PHPUnit\Framework\TestCase
  10. {
  11. const SAMPLE_CONFIG_PATH = 'web/unsecure/base_url';
  12. const SAMPLE_VALUE = 'http://example.com/';
  13. /**
  14. * @var \Magento\Framework\App\Config\Value
  15. */
  16. protected $_model;
  17. public static function setUpBeforeClass()
  18. {
  19. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  20. \Magento\Framework\App\Config\Storage\WriterInterface::class
  21. )->save(
  22. self::SAMPLE_CONFIG_PATH,
  23. self::SAMPLE_VALUE
  24. );
  25. self::_refreshConfiguration();
  26. }
  27. public static function tearDownAfterClass()
  28. {
  29. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
  30. \Magento\Framework\App\Config\Storage\WriterInterface::class
  31. )->delete(
  32. self::SAMPLE_CONFIG_PATH
  33. );
  34. self::_refreshConfiguration();
  35. }
  36. /**
  37. * Remove cached configuration and reinitialize the application
  38. */
  39. protected static function _refreshConfiguration()
  40. {
  41. \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Framework\App\CacheInterface::class)
  42. ->clean([\Magento\Framework\App\Config::CACHE_TAG]);
  43. \Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize();
  44. $appConfig = ObjectManager::getInstance()->get(Config::class);
  45. $appConfig->clean();
  46. }
  47. protected function setUp()
  48. {
  49. $this->_model = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  50. \Magento\Framework\App\Config\Value::class
  51. );
  52. }
  53. public function testIsValueChanged()
  54. {
  55. // load the model
  56. $collection = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  57. \Magento\Config\Model\ResourceModel\Config\Data\Collection::class
  58. );
  59. $collection->addFieldToFilter(
  60. 'path',
  61. self::SAMPLE_CONFIG_PATH
  62. )->addFieldToFilter(
  63. 'scope_id',
  64. 0
  65. )->addFieldToFilter(
  66. 'scope',
  67. 'default'
  68. );
  69. foreach ($collection as $configData) {
  70. $this->_model = $configData;
  71. break;
  72. }
  73. $this->assertNotEmpty($this->_model->getId());
  74. // assert
  75. $this->assertFalse($this->_model->isValueChanged());
  76. $this->_model->setValue(uniqid());
  77. $this->assertTrue($this->_model->isValueChanged());
  78. }
  79. public function testGetOldValue()
  80. {
  81. $this->_model->setPath(self::SAMPLE_CONFIG_PATH);
  82. $this->assertEquals(self::SAMPLE_VALUE, $this->_model->getOldValue());
  83. $this->_model->setWebsiteCode('base');
  84. $this->assertEquals(self::SAMPLE_VALUE, $this->_model->getOldValue());
  85. $this->_model->setStoreCode('default');
  86. $this->assertEquals(self::SAMPLE_VALUE, $this->_model->getOldValue());
  87. }
  88. public function testGetFieldsetDataValue()
  89. {
  90. $this->assertNull($this->_model->getFieldsetDataValue('key'));
  91. $this->_model->setFieldsetData(['key' => 'value']);
  92. $this->assertEquals('value', $this->_model->getFieldsetDataValue('key'));
  93. }
  94. public function testCRUD()
  95. {
  96. $this->_model->setData(
  97. ['scope' => 'default', 'scope_id' => 0, 'path' => 'test/config/path', 'value' => 'test value']
  98. );
  99. $crud = new \Magento\TestFramework\Entity($this->_model, ['value' => 'new value']);
  100. $crud->testCrud();
  101. }
  102. public function testCollection()
  103. {
  104. $collection = $this->_model->getCollection();
  105. $collection->addScopeFilter(
  106. 'test',
  107. 0,
  108. 'test'
  109. )->addPathFilter(
  110. 'not_existing_path'
  111. )->addValueFilter(
  112. 'not_existing_value'
  113. );
  114. $this->assertEmpty($collection->getItems());
  115. }
  116. }