ExceptionsTest.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Design\Backend;
  7. use Magento\Framework\Serialize\Serializer\Json;
  8. class ExceptionsTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Theme\Model\Design\Backend\Exceptions
  12. */
  13. private $exceptions = null;
  14. /** @var Json */
  15. private $serializer;
  16. protected function setUp()
  17. {
  18. $this->exceptions = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
  19. \Magento\Theme\Model\Design\Backend\Exceptions::class
  20. );
  21. $this->exceptions->setScope('default');
  22. $this->exceptions->setScopeId(0);
  23. $this->exceptions->setPath('design/theme/ua_regexp');
  24. $this->serializer = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(Json::class);
  25. }
  26. /**
  27. * Basic test, checks that saved value contains all required entries and is saved as an array
  28. * @magentoDbIsolation enabled
  29. */
  30. public function testSaveValueIsFormedNicely()
  31. {
  32. $value = [
  33. '1' => ['search' => '/Opera/', 'value' => 'Magento/blank'],
  34. '2' => ['search' => '/Firefox/', 'value' => 'Magento/blank'],
  35. ];
  36. $this->exceptions->setValue($value);
  37. $this->exceptions->save();
  38. $processedValue = $this->serializer->unserialize($this->exceptions->getValue());
  39. $this->assertEquals(count($processedValue), 2, 'Number of saved values is wrong');
  40. $entry = $processedValue['1'];
  41. $this->assertArrayHasKey('search', $entry);
  42. $this->assertArrayHasKey('value', $entry);
  43. $this->assertArrayHasKey('regexp', $entry);
  44. }
  45. /**
  46. * @magentoDbIsolation enabled
  47. */
  48. public function testSaveEmptyValueIsSkipped()
  49. {
  50. $value = [
  51. '1' => ['search' => '/Opera/', 'value' => 'Magento/blank'],
  52. '2' => ['search' => '', 'value' => 'Magento/blank'],
  53. '3' => ['search' => '/Firefox/', 'value' => 'Magento/blank'],
  54. ];
  55. $this->exceptions->setValue($value);
  56. $this->exceptions->save();
  57. $processedValue = $this->serializer->unserialize($this->exceptions->getValue());
  58. $emptyIsSkipped = isset($processedValue['1']) && !isset($processedValue['2']) && isset($processedValue['3']);
  59. $this->assertTrue($emptyIsSkipped);
  60. }
  61. /**
  62. * @param array $designException
  63. * @param string $regexp
  64. * @dataProvider saveExceptionDataProvider
  65. * @magentoDbIsolation enabled
  66. */
  67. public function testSaveException($designException, $regexp)
  68. {
  69. $this->exceptions->setValue(['1' => $designException]);
  70. $this->exceptions->save();
  71. $processedValue = $this->serializer->unserialize($this->exceptions->getValue());
  72. $this->assertEquals($processedValue['1']['regexp'], $regexp);
  73. }
  74. /**
  75. * @return array
  76. */
  77. public function saveExceptionDataProvider()
  78. {
  79. $result = [
  80. [['search' => 'Opera', 'value' => 'Magento/blank'], '/Opera/i'],
  81. [['search' => '/Opera/', 'value' => 'Magento/blank'], '/Opera/'],
  82. [['search' => '#iPad|iPhone#i', 'value' => 'Magento/blank'], '#iPad|iPhone#i'],
  83. [
  84. ['search' => 'Mozilla (3.6+)/Firefox', 'value' => 'Magento/blank'],
  85. '/Mozilla \\(3\\.6\\+\\)\\/Firefox/i'
  86. ],
  87. ];
  88. return $result;
  89. }
  90. /**
  91. * @var array $value
  92. * @expectedException \Magento\Framework\Exception\LocalizedException
  93. * @dataProvider saveWrongExceptionDataProvider
  94. * @magentoDbIsolation enabled
  95. */
  96. public function testSaveWrongException($value)
  97. {
  98. $this->exceptions->setValue($value);
  99. $this->exceptions->save();
  100. }
  101. /**
  102. * @return array
  103. */
  104. public function saveWrongExceptionDataProvider()
  105. {
  106. $result = [
  107. [
  108. [
  109. '1' => ['search' => '/Opera/', 'value' => 'Magento/blank'],
  110. '2' => ['search' => '/invalid_regexp(/', 'value' => 'Magento/blank'],
  111. ],
  112. ],
  113. [
  114. [
  115. '1' => ['search' => '/invalid_regexp', 'value' => 'Magento/blank'],
  116. '2' => ['search' => '/Opera/', 'value' => 'Magento/blank'],
  117. ]
  118. ],
  119. [
  120. [
  121. '1' => ['search' => 'invalid_regexp/iU', 'value' => 'Magento/blank'],
  122. '2' => ['search' => '/Opera/', 'value' => 'Magento/blank'],
  123. ]
  124. ],
  125. [
  126. [
  127. '1' => ['search' => 'invalid_regexp#', 'value' => 'Magento/blank'],
  128. '2' => ['search' => '/Opera/', 'value' => 'Magento/blank'],
  129. ]
  130. ],
  131. [
  132. [
  133. '1' => ['search' => '/Firefox/'],
  134. '2' => ['search' => '/Opera/', 'value' => 'Magento/blank'],
  135. ]
  136. ],
  137. [
  138. [
  139. '1' => ['value' => 'Magento/blank'],
  140. '2' => ['search' => '/Opera/', 'value' => 'Magento/blank'],
  141. ]
  142. ],
  143. ];
  144. return $result;
  145. }
  146. }