Skip to content

Commit 80cfc05

Browse files
committed
Merge remote-tracking branch 'origin/MAGETWO-37720' into sprint-54-bf
2 parents cb1d5a3 + f2a88e0 commit 80cfc05

File tree

6 files changed

+1661
-0
lines changed

6 files changed

+1661
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Cms\Test\Unit\Block\Widget\Page;
7+
8+
class LinkTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Cms\Block\Widget\Page\Link
12+
*/
13+
protected $linkElement;
14+
15+
/**
16+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
17+
*/
18+
protected $objectManager;
19+
20+
/**
21+
* @var \Magento\Cms\Helper\Page|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
protected $mockCmsPage;
24+
25+
/**
26+
* @var \Magento\Cms\Model\Resource\Page|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
protected $mockResourcePage;
29+
30+
protected function setUp()
31+
{
32+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
33+
$this->mockCmsPage = $this->getMock('Magento\Cms\Helper\Page', [], [], '', false, false);
34+
$this->mockResourcePage = $this->getMock('Magento\Cms\Model\Resource\Page', [], [], '', false, false);
35+
36+
$this->linkElement = $this->objectManager->getObject(
37+
'Magento\Cms\Block\Widget\Page\Link',
38+
[
39+
'cmsPage' => $this->mockCmsPage,
40+
'resourcePage' => $this->mockResourcePage,
41+
]
42+
);
43+
}
44+
45+
protected function tearDown()
46+
{
47+
$this->linkElement = null;
48+
}
49+
50+
public function testGetHrefEmpty()
51+
{
52+
$this->assertEmpty($this->linkElement->getHref());
53+
}
54+
55+
public function testGetHref()
56+
{
57+
$href = 'localhost';
58+
$this->linkElement->setData('href', $href);
59+
$this->assertEquals($href, $this->linkElement->getHref());
60+
}
61+
62+
public function testGetHrefByPageId()
63+
{
64+
$href = 'pagelink';
65+
$this->mockCmsPage->expects($this->once())
66+
->method('getPageUrl')
67+
->willReturn($href);
68+
$this->linkElement->setData('page_id', 1);
69+
$this->assertEquals($href, $this->linkElement->getHref());
70+
}
71+
72+
public function testGetTitleEmpty()
73+
{
74+
$this->assertEmpty($this->linkElement->getTitle());
75+
}
76+
77+
public function testGetTitle()
78+
{
79+
$title = 'Title';
80+
$this->linkElement->setData('title', $title);
81+
$this->assertEquals($title, $this->linkElement->getTitle());
82+
}
83+
84+
public function testGetTitleByPageId()
85+
{
86+
$pageId = 1;
87+
$title = 'Title by page id';
88+
$this->mockResourcePage->expects($this->once())
89+
->method('getCmsPageTitleById')
90+
->with($pageId)
91+
->willReturn($title);
92+
$this->linkElement->setData('page_id', $pageId);
93+
$this->assertEquals($title, $this->linkElement->getTitle());
94+
}
95+
96+
public function testGetTitleByHref()
97+
{
98+
$href = 'localhost';
99+
$title = 'Title by href';
100+
$this->mockResourcePage->expects($this->once())
101+
->method('setStore')
102+
->willReturnSelf();
103+
$this->mockResourcePage->expects($this->once())
104+
->method('getCmsPageTitleByIdentifier')
105+
->with($href)
106+
->willReturn($title);
107+
$this->linkElement->setData('href', $href);
108+
$this->assertEquals($title, $this->linkElement->getTitle());
109+
}
110+
111+
public function testGetLabelByAnchorText()
112+
{
113+
$label = 'Test label';
114+
$this->linkElement->setData('anchor_text', $label);
115+
$this->assertEquals($label, $this->linkElement->getLabel());
116+
}
117+
118+
public function testGetLabelLikeTitle()
119+
{
120+
$label = 'Test title';
121+
$this->linkElement->setData('title', $label);
122+
$this->assertEquals($label, $this->linkElement->getLabel());
123+
}
124+
125+
public function testGetLabelByHref()
126+
{
127+
$href = 'localhost';
128+
$label = 'Label by href';
129+
$this->mockResourcePage->expects($this->once())
130+
->method('setStore')
131+
->willReturnSelf();
132+
$this->mockResourcePage->expects($this->once())
133+
->method('getCmsPageTitleByIdentifier')
134+
->with($href)
135+
->willReturn($label);
136+
$this->linkElement->setData('href', $href);
137+
$this->assertEquals($label, $this->linkElement->getLabel());
138+
}
139+
140+
public function testGetLabelByPageId()
141+
{
142+
$pageId = 1;
143+
$label = 'Label by page id';
144+
$this->mockResourcePage->expects($this->once())
145+
->method('getCmsPageTitleById')
146+
->with($pageId)
147+
->willReturn($label);
148+
$this->linkElement->setData('page_id', $pageId);
149+
$this->assertEquals($label, $this->linkElement->getLabel());
150+
}
151+
152+
public function testGetLabelEmpty()
153+
{
154+
$this->assertEmpty($this->linkElement->getLabel());
155+
}
156+
}
Lines changed: 200 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Cms\Test\Unit\Controller\Adminhtml\Block;
7+
8+
/**
9+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
10+
*/
11+
class DeleteTest extends \PHPUnit_Framework_TestCase
12+
{
13+
/**
14+
* @var \Magento\Cms\Controller\Adminhtml\Block\Delete
15+
*/
16+
protected $deleteController;
17+
18+
/**
19+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
20+
*/
21+
protected $objectManager;
22+
23+
/**
24+
* @var \Magento\Backend\App\Action\Context|\PHPUnit_Framework_MockObject_MockObject
25+
*/
26+
protected $contextMock;
27+
28+
/**
29+
* @var \Magento\Backend\Model\View\Result\RedirectFactory|\PHPUnit_Framework_MockObject_MockObject
30+
*/
31+
protected $resultRedirectFactoryMock;
32+
33+
/**
34+
* @var \Magento\Backend\Model\View\Result\Redirect|\PHPUnit_Framework_MockObject_MockObject
35+
*/
36+
protected $resultRedirectMock;
37+
38+
/**
39+
* @var \Magento\Framework\Message\ManagerInterface|\PHPUnit_Framework_MockObject_MockObject
40+
*/
41+
protected $messageManagerMock;
42+
43+
/**
44+
* @var \Magento\Framework\App\RequestInterface|\PHPUnit_Framework_MockObject_MockObject
45+
*/
46+
protected $requestMock;
47+
48+
/**
49+
* @var \Magento\Framework\ObjectManager\ObjectManager|\PHPUnit_Framework_MockObject_MockObject
50+
*/
51+
protected $objectManagerMock;
52+
53+
/**
54+
* @var \Magento\Cms\Model\Block|\PHPUnit_Framework_MockObject_MockObject $blockMock
55+
*/
56+
protected $blockMock;
57+
58+
/**
59+
* @var int
60+
*/
61+
protected $blockId = 1;
62+
63+
protected function setUp()
64+
{
65+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
66+
67+
$this->messageManagerMock = $this->getMock('Magento\Framework\Message\ManagerInterface', [], [], '', false);
68+
69+
$this->requestMock = $this->getMockForAbstractClass(
70+
'Magento\Framework\App\RequestInterface',
71+
[],
72+
'',
73+
false,
74+
true,
75+
true,
76+
['getParam']
77+
);
78+
79+
$this->blockMock = $this->getMockBuilder('Magento\Cms\Model\Block')
80+
->disableOriginalConstructor()
81+
->setMethods(['load', 'delete'])
82+
->getMock();
83+
84+
$this->objectManagerMock = $this->getMockBuilder('Magento\Framework\ObjectManager\ObjectManager')
85+
->disableOriginalConstructor()
86+
->setMethods(['create'])
87+
->getMock();
88+
89+
$this->resultRedirectMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\Redirect')
90+
->setMethods(['setPath'])
91+
->disableOriginalConstructor()
92+
->getMock();
93+
94+
$this->resultRedirectFactoryMock = $this->getMockBuilder('Magento\Backend\Model\View\Result\RedirectFactory')
95+
->disableOriginalConstructor()
96+
->setMethods(['create'])
97+
->getMock();
98+
$this->resultRedirectFactoryMock->expects($this->atLeastOnce())
99+
->method('create')
100+
->willReturn($this->resultRedirectMock);
101+
102+
$this->contextMock = $this->getMock(
103+
'\Magento\Backend\App\Action\Context',
104+
[],
105+
[],
106+
'',
107+
false
108+
);
109+
110+
$this->contextMock->expects($this->any())->method('getRequest')->willReturn($this->requestMock);
111+
$this->contextMock->expects($this->any())->method('getMessageManager')->willReturn($this->messageManagerMock);
112+
$this->contextMock->expects($this->any())->method('getObjectManager')->willReturn($this->objectManagerMock);
113+
$this->contextMock->expects($this->any())
114+
->method('getResultRedirectFactory')
115+
->willReturn($this->resultRedirectFactoryMock);
116+
117+
$this->deleteController = $this->objectManager->getObject(
118+
'Magento\Cms\Controller\Adminhtml\Block\Delete',
119+
[
120+
'context' => $this->contextMock,
121+
]
122+
);
123+
}
124+
125+
public function testDeleteAction()
126+
{
127+
$this->requestMock->expects($this->once())
128+
->method('getParam')
129+
->willReturn($this->blockId);
130+
131+
$this->objectManagerMock->expects($this->once())
132+
->method('create')
133+
->with('Magento\Cms\Model\Block')
134+
->willReturn($this->blockMock);
135+
136+
$this->blockMock->expects($this->once())
137+
->method('load')
138+
->with($this->blockId);
139+
140+
$this->messageManagerMock->expects($this->once())
141+
->method('addSuccess')
142+
->with(__('The block has been deleted.'));
143+
$this->messageManagerMock->expects($this->never())
144+
->method('addError');
145+
146+
$this->resultRedirectMock->expects($this->once())
147+
->method('setPath')
148+
->with('*/*/')
149+
->willReturnSelf();
150+
151+
$this->assertSame($this->resultRedirectMock, $this->deleteController->execute());
152+
}
153+
154+
public function testDeleteActionNoId()
155+
{
156+
$this->requestMock->expects($this->once())
157+
->method('getParam')
158+
->willReturn(null);
159+
160+
$this->messageManagerMock->expects($this->once())
161+
->method('addError')
162+
->with(__('We can\'t find a block to delete.'));
163+
$this->messageManagerMock->expects($this->never())
164+
->method('addSuccess');
165+
166+
$this->resultRedirectMock->expects($this->once())
167+
->method('setPath')
168+
->with('*/*/')
169+
->willReturnSelf();
170+
171+
$this->assertSame($this->resultRedirectMock, $this->deleteController->execute());
172+
}
173+
174+
public function testDeleteActionThrowsException()
175+
{
176+
$errorMsg = 'Can\'t create the block';
177+
178+
$this->requestMock->expects($this->once())
179+
->method('getParam')
180+
->willReturn($this->blockId);
181+
182+
$this->objectManagerMock->expects($this->once())
183+
->method('create')
184+
->with('Magento\Cms\Model\Block')
185+
->willThrowException(new \Exception(__($errorMsg)));
186+
187+
$this->messageManagerMock->expects($this->once())
188+
->method('addError')
189+
->with($errorMsg);
190+
$this->messageManagerMock->expects($this->never())
191+
->method('addSuccess');
192+
193+
$this->resultRedirectMock->expects($this->once())
194+
->method('setPath')
195+
->with('*/*/edit', ['block_id' => $this->blockId])
196+
->willReturnSelf();
197+
198+
$this->assertSame($this->resultRedirectMock, $this->deleteController->execute());
199+
}
200+
}

0 commit comments

Comments
 (0)