InlineEditTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Cms\Test\Unit\Controller\Block;
  7. use Magento\Cms\Controller\Adminhtml\Block\InlineEdit;
  8. class InlineEditTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /** @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
  11. protected $request;
  12. /** @var \Magento\Cms\Model\Block|\PHPUnit_Framework_MockObject_MockObject */
  13. protected $cmsBlock;
  14. /** @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject */
  15. protected $context;
  16. /** @var \Magento\Cms\Api\BlockRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */
  17. protected $blockRepository;
  18. /** @var \Magento\Framework\Controller\Result\JsonFactory|\PHPUnit_Framework_MockObject_MockObject */
  19. protected $jsonFactory;
  20. /** @var \Magento\Framework\Controller\Result\Json|\PHPUnit_Framework_MockObject_MockObject */
  21. protected $resultJson;
  22. /** @var InlineEdit */
  23. protected $controller;
  24. protected function setUp()
  25. {
  26. $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  27. $this->request = $this->getMockForAbstractClass(
  28. \Magento\Framework\App\RequestInterface::class,
  29. [],
  30. '',
  31. false
  32. );
  33. $this->cmsBlock = $this->createMock(\Magento\Cms\Model\Block::class);
  34. $this->context = $helper->getObject(
  35. \Magento\Backend\App\Action\Context::class,
  36. [
  37. 'request' => $this->request
  38. ]
  39. );
  40. $this->blockRepository = $this->getMockForAbstractClass(
  41. \Magento\Cms\Api\BlockRepositoryInterface::class,
  42. [],
  43. '',
  44. false
  45. );
  46. $this->resultJson = $this->createMock(\Magento\Framework\Controller\Result\Json::class);
  47. $this->jsonFactory = $this->createPartialMock(
  48. \Magento\Framework\Controller\Result\JsonFactory::class,
  49. ['create']
  50. );
  51. $this->controller = new InlineEdit(
  52. $this->context,
  53. $this->blockRepository,
  54. $this->jsonFactory
  55. );
  56. }
  57. public function prepareMocksForTestExecute()
  58. {
  59. $postData = [
  60. 1 => [
  61. 'title' => 'Catalog Events Lister',
  62. 'identifier' => 'Catalog Events Lister'
  63. ]
  64. ];
  65. $this->request->expects($this->at(0))
  66. ->method('getParam')
  67. ->with('isAjax')
  68. ->willReturn(true);
  69. $this->request->expects($this->at(1))
  70. ->method('getParam')
  71. ->with('items', [])
  72. ->willReturn($postData);
  73. $this->blockRepository->expects($this->once())
  74. ->method('getById')
  75. ->with(1)
  76. ->willReturn($this->cmsBlock);
  77. $this->cmsBlock->expects($this->atLeastOnce())
  78. ->method('getId')
  79. ->willReturn('1');
  80. $this->cmsBlock->expects($this->once())
  81. ->method('getData')
  82. ->willReturn([
  83. 'identifier' => 'test-identifier'
  84. ]);
  85. $this->cmsBlock->expects($this->once())
  86. ->method('setData')
  87. ->with([
  88. 'title' => 'Catalog Events Lister',
  89. 'identifier' => 'Catalog Events Lister'
  90. ]);
  91. $this->jsonFactory->expects($this->once())
  92. ->method('create')
  93. ->willReturn($this->resultJson);
  94. }
  95. public function testExecuteWithException()
  96. {
  97. $this->prepareMocksForTestExecute();
  98. $this->blockRepository->expects($this->once())
  99. ->method('save')
  100. ->with($this->cmsBlock)
  101. ->willThrowException(new \Exception(__('Exception')));
  102. $this->resultJson->expects($this->once())
  103. ->method('setData')
  104. ->with([
  105. 'messages' => [
  106. '[Block ID: 1] Exception'
  107. ],
  108. 'error' => true
  109. ])
  110. ->willReturnSelf();
  111. $this->controller->execute();
  112. }
  113. public function testExecuteWithoutData()
  114. {
  115. $this->request->expects($this->at(0))
  116. ->method('getParam')
  117. ->with('isAjax')
  118. ->willReturn(true);
  119. $this->request->expects($this->at(1))
  120. ->method('getParam')
  121. ->with('items', [])
  122. ->willReturn([]);
  123. $this->jsonFactory->expects($this->once())
  124. ->method('create')
  125. ->willReturn($this->resultJson);
  126. $this->resultJson->expects($this->once())
  127. ->method('setData')
  128. ->with([
  129. 'messages' => [
  130. 'Please correct the data sent.'
  131. ],
  132. 'error' => true
  133. ])
  134. ->willReturnSelf();
  135. $this->controller->execute();
  136. }
  137. }