ValidatorTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Theme\Model\Config;
  7. use Magento\Email\Model\Template;
  8. /**
  9. * Class ValidatorTest to test \Magento\Theme\Model\Design\Config\Validator
  10. */
  11. class ValidatorTest extends \PHPUnit\Framework\TestCase
  12. {
  13. const TEMPLATE_CODE = 'email_exception_fixture';
  14. /**
  15. * @var \Magento\Theme\Model\Design\Config\Validator
  16. */
  17. private $model;
  18. /**
  19. * @var \PHPUnit_Framework_MockObject_MockObject
  20. */
  21. private $templateFactoryMock;
  22. /**
  23. * @var \Magento\Email\Model\Template
  24. */
  25. private $templateModel;
  26. protected function setUp()
  27. {
  28. $objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  29. $objectManager->get(\Magento\Framework\App\AreaList::class)
  30. ->getArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE)
  31. ->load(\Magento\Framework\App\Area::PART_CONFIG);
  32. $objectManager->get(\Magento\Framework\App\State::class)
  33. ->setAreaCode(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
  34. $this->templateFactoryMock = $this->getMockBuilder(\Magento\Framework\Mail\TemplateInterfaceFactory::class)
  35. ->disableOriginalConstructor()
  36. ->getMock();
  37. $this->templateModel = $objectManager->create(\Magento\Email\Model\Template::class);
  38. $this->templateModel->load(self::TEMPLATE_CODE, 'template_code');
  39. $this->templateFactoryMock->expects($this->once())
  40. ->method("create")
  41. ->willReturn($this->templateModel);
  42. $this->model = $objectManager->create(
  43. \Magento\Theme\Model\Design\Config\Validator::class,
  44. [ 'templateFactory' => $this->templateFactoryMock ]
  45. );
  46. }
  47. /**
  48. * @magentoDataFixture Magento/Email/Model/_files/email_template.php
  49. * @expectedException \Magento\Framework\Exception\LocalizedException
  50. */
  51. public function testValidateHasRecursiveReference()
  52. {
  53. if (!$this->templateModel->getId()) {
  54. $this->fail('Cannot load Template model');
  55. }
  56. $fieldConfig = [
  57. 'path' => 'design/email/header_template',
  58. 'fieldset' => 'other_settings/email',
  59. 'field' => 'email_header_template'
  60. ];
  61. $designConfigMock = $this->getMockBuilder(\Magento\Theme\Api\Data\DesignConfigInterface::class)
  62. ->disableOriginalConstructor()
  63. ->setMethods([])
  64. ->getMock();
  65. $designConfigExtensionMock =
  66. $this->getMockBuilder(\Magento\Theme\Api\Data\DesignConfigExtensionInterface::class)
  67. ->disableOriginalConstructor()
  68. ->setMethods([])
  69. ->getMock();
  70. $designElementMock = $this->getMockBuilder(\Magento\Theme\Model\Data\Design\Config\Data::class)
  71. ->disableOriginalConstructor()
  72. ->setMethods([])
  73. ->getMock();
  74. $designConfigMock->expects($this->once())
  75. ->method('getExtensionAttributes')
  76. ->willReturn($designConfigExtensionMock);
  77. $designConfigExtensionMock->expects($this->once())
  78. ->method('getDesignConfigData')
  79. ->willReturn([$designElementMock]);
  80. $designElementMock->expects($this->any())->method('getFieldConfig')->willReturn($fieldConfig);
  81. $designElementMock->expects($this->once())->method('getPath')->willReturn($fieldConfig['path']);
  82. $designElementMock->expects($this->once())->method('getValue')->willReturn($this->templateModel->getId());
  83. $this->model->validate($designConfigMock);
  84. $this->expectExceptionMessage(
  85. 'The "email_header_template" template contains an incorrect configuration, with a reference to itself. '
  86. . 'Remove or change the reference, then try again.'
  87. );
  88. }
  89. /**
  90. * @magentoDataFixture Magento/Email/Model/_files/email_template.php
  91. */
  92. public function testValidateNoRecursiveReference()
  93. {
  94. $this->templateFactoryMock->expects($this->once())
  95. ->method("create")
  96. ->willReturn($this->templateModel);
  97. $fieldConfig = [
  98. 'path' => 'design/email/footer_template',
  99. 'fieldset' => 'other_settings/email',
  100. 'field' => 'email_footer_template'
  101. ];
  102. $designConfigMock = $this->getMockBuilder(\Magento\Theme\Api\Data\DesignConfigInterface::class)
  103. ->disableOriginalConstructor()
  104. ->setMethods([])
  105. ->getMock();
  106. $designConfigExtensionMock =
  107. $this->getMockBuilder(\Magento\Theme\Api\Data\DesignConfigExtensionInterface::class)
  108. ->disableOriginalConstructor()
  109. ->setMethods([])
  110. ->getMock();
  111. $designElementMock = $this->getMockBuilder(\Magento\Theme\Model\Data\Design\Config\Data::class)
  112. ->disableOriginalConstructor()
  113. ->setMethods([])
  114. ->getMock();
  115. $designConfigMock->expects($this->once())
  116. ->method('getExtensionAttributes')
  117. ->willReturn($designConfigExtensionMock);
  118. $designConfigExtensionMock->expects($this->once())
  119. ->method('getDesignConfigData')
  120. ->willReturn([$designElementMock]);
  121. $designElementMock->expects($this->any())->method('getFieldConfig')->willReturn($fieldConfig);
  122. $designElementMock->expects($this->once())->method('getPath')->willReturn($fieldConfig['path']);
  123. $designElementMock->expects($this->once())->method('getValue')->willReturn($this->templateModel->getId());
  124. $this->model->validate($designConfigMock);
  125. }
  126. }