Skip to content

Commit 6bbfcaa

Browse files
author
Victor Rad
committed
Merge remote-tracking branch 'origin/MAGETWO-38011' into develop
2 parents f675c4c + 75a1a76 commit 6bbfcaa

File tree

5 files changed

+503
-18
lines changed

5 files changed

+503
-18
lines changed

app/code/Magento/Email/Model/AbstractTemplate.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,14 +79,16 @@ public function __construct(
7979
\Magento\Framework\Registry $registry,
8080
\Magento\Store\Model\App\Emulation $appEmulation,
8181
\Magento\Store\Model\StoreManagerInterface $storeManager,
82+
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
83+
\Magento\Framework\Data\Collection\Db $resourceCollection = null,
8284
array $data = []
8385
) {
8486
$this->_design = $design;
8587
$this->_area = isset($data['area']) ? $data['area'] : null;
8688
$this->_store = isset($data['store']) ? $data['store'] : null;
8789
$this->_appEmulation = $appEmulation;
8890
$this->_storeManager = $storeManager;
89-
parent::__construct($context, $registry, null, null, $data);
91+
parent::__construct($context, $registry, $resource, $resourceCollection, $data);
9092
}
9193

9294
/**

app/code/Magento/Newsletter/Model/Queue.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,20 @@ public function __construct(
134134
\Magento\Newsletter\Model\ProblemFactory $problemFactory,
135135
\Magento\Newsletter\Model\Resource\Subscriber\CollectionFactory $subscriberCollectionFactory,
136136
\Magento\Newsletter\Model\Queue\TransportBuilder $transportBuilder,
137+
\Magento\Framework\Model\Resource\AbstractResource $resource = null,
138+
\Magento\Framework\Data\Collection\Db $resourceCollection = null,
137139
array $data = []
138140
) {
139-
parent::__construct($context, $design, $registry, $appEmulation, $storeManager, $data);
141+
parent::__construct(
142+
$context,
143+
$design,
144+
$registry,
145+
$appEmulation,
146+
$storeManager,
147+
$resource,
148+
$resourceCollection,
149+
$data
150+
);
140151
$this->_templateFilter = $templateFilter;
141152
$this->_date = $date;
142153
$this->_templateFactory = $templateFactory;
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Newsletter\Test\Unit\Model\Plugin;
7+
8+
class CustomerPluginTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Newsletter\Model\Plugin\CustomerPlugin
12+
*/
13+
protected $plugin;
14+
15+
/**
16+
* @var \Magento\Newsletter\Model\SubscriberFactory|\PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
private $subscriberFactory;
19+
20+
/**
21+
* @var \Magento\Newsletter\Model\Subscriber|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
private $subscriber;
24+
25+
/**
26+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
27+
*/
28+
protected $objectManager;
29+
30+
public function setUp()
31+
{
32+
$this->subscriberFactory = $this->getMockBuilder('\Magento\Newsletter\Model\SubscriberFactory')
33+
->disableOriginalConstructor()
34+
->setMethods(['create'])
35+
->getMock();
36+
$this->subscriber = $this->getMockBuilder('\Magento\Newsletter\Model\Subscriber')
37+
->setMethods(['loadByEmail', 'getId', 'delete', 'updateSubscription'])
38+
->disableOriginalConstructor()
39+
->getMock();
40+
$this->subscriberFactory->expects($this->any())->method('create')->willReturn($this->subscriber);
41+
42+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
43+
44+
$this->plugin = $this->objectManager->getObject(
45+
'Magento\Newsletter\Model\Plugin\CustomerPlugin',
46+
[
47+
'subscriberFactory' => $this->subscriberFactory
48+
]
49+
);
50+
}
51+
52+
public function testAfterSave()
53+
{
54+
$customerId = 1;
55+
$subject = $this->getMock('\Magento\Customer\Api\CustomerRepositoryInterface');
56+
$customer = $this->getMock('Magento\Customer\Api\Data\CustomerInterface');
57+
$customer->expects($this->once())->method('getId')->willReturn($customerId);
58+
$this->subscriber->expects($this->once())->method('updateSubscription')->with($customerId)->willReturnSelf();
59+
60+
$this->assertEquals($customer, $this->plugin->afterSave($subject, $customer));
61+
}
62+
63+
public function testAroundDelete()
64+
{
65+
$deleteCustomer = function () {
66+
return true;
67+
};
68+
$subject = $this->getMock('\Magento\Customer\Api\CustomerRepositoryInterface');
69+
$customer = $this->getMock('Magento\Customer\Api\Data\CustomerInterface');
70+
$customer->expects($this->once())->method('getEmail')->willReturn('test@test.com');
71+
$this->subscriber->expects($this->once())->method('loadByEmail')->with('test@test.com')->willReturnSelf();
72+
$this->subscriber->expects($this->once())->method('getId')->willReturn(1);
73+
$this->subscriber->expects($this->once())->method('delete')->willReturnSelf();
74+
75+
$this->assertEquals(true, $this->plugin->aroundDelete($subject, $deleteCustomer, $customer));
76+
}
77+
78+
public function testAroundDeleteById()
79+
{
80+
$customerId = 1;
81+
$deleteCustomerById = function () {
82+
return true;
83+
};
84+
$subject = $this->getMock('\Magento\Customer\Api\CustomerRepositoryInterface');
85+
$customer = $this->getMock('Magento\Customer\Api\Data\CustomerInterface');
86+
$subject->expects($this->once())->method('getById')->willReturn($customer);
87+
$customer->expects($this->once())->method('getEmail')->willReturn('test@test.com');
88+
$this->subscriber->expects($this->once())->method('loadByEmail')->with('test@test.com')->willReturnSelf();
89+
$this->subscriber->expects($this->once())->method('getId')->willReturn(1);
90+
$this->subscriber->expects($this->once())->method('delete')->willReturnSelf();
91+
92+
$this->assertEquals(true, $this->plugin->aroundDeleteById($subject, $deleteCustomerById, $customerId));
93+
}
94+
}
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
/**
3+
* Copyright © 2015 Magento. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Newsletter\Test\Unit\Model;
7+
8+
class QueueTest extends \PHPUnit_Framework_TestCase
9+
{
10+
/**
11+
* @var \Magento\Newsletter\Model\Queue
12+
*/
13+
protected $queue;
14+
15+
/**
16+
* @var \Magento\Newsletter\Model\Template\Filter|\PHPUnit_Framework_MockObject_MockObject
17+
*/
18+
protected $templateFilter;
19+
20+
/**
21+
* @var \Magento\Framework\Stdlib\DateTime\DateTime|\PHPUnit_Framework_MockObject_MockObject
22+
*/
23+
protected $date;
24+
25+
/**
26+
* @var \Magento\Newsletter\Model\TemplateFactory|\PHPUnit_Framework_MockObject_MockObject
27+
*/
28+
protected $templateFactory;
29+
30+
/**
31+
* @var \Magento\Newsletter\Model\ProblemFactory|\PHPUnit_Framework_MockObject_MockObject
32+
*/
33+
protected $problemFactory;
34+
35+
/**
36+
* @var \Magento\Newsletter\Model\Resource\Subscriber\Collection|\PHPUnit_Framework_MockObject_MockObject
37+
*/
38+
protected $subscribersCollection;
39+
40+
/**
41+
* @var \Magento\Newsletter\Model\Resource\Subscriber\CollectionFactory|\PHPUnit_Framework_MockObject_MockObject
42+
*/
43+
protected $subscribersCollectionFactory;
44+
45+
/**
46+
* @var \Magento\Newsletter\Model\Queue\TransportBuilder|\PHPUnit_Framework_MockObject_MockObject
47+
*/
48+
protected $transportBuilder;
49+
50+
/**
51+
* @var \Magento\Newsletter\Model\Resource\Queue|\PHPUnit_Framework_MockObject_MockObject
52+
*/
53+
protected $resource;
54+
55+
/**
56+
* @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
57+
*/
58+
protected $objectManager;
59+
60+
public function setUp()
61+
{
62+
$this->templateFilter = $this->getMockBuilder('\Magento\Newsletter\Model\Template\Filter')
63+
->disableOriginalConstructor()
64+
->setMethods(['create'])
65+
->getMock();
66+
$this->date = $this->getMockBuilder('\Magento\Framework\Stdlib\DateTime\DateTime')
67+
->disableOriginalConstructor()
68+
->getMock();
69+
$this->templateFactory = $this->getMockBuilder('\Magento\Newsletter\Model\TemplateFactory')
70+
->disableOriginalConstructor()
71+
->setMethods(['create', 'load'])
72+
->getMock();
73+
$this->problemFactory = $this->getMockBuilder('\Magento\Newsletter\Model\ProblemFactory')
74+
->disableOriginalConstructor()
75+
->getMock();
76+
$this->transportBuilder = $this->getMockBuilder('\Magento\Newsletter\Model\Queue\TransportBuilder')
77+
->disableOriginalConstructor()
78+
->setMethods(
79+
['setTemplateData', 'setTemplateOptions', 'setTemplateVars', 'setFrom', 'addTo', 'getTransport']
80+
)
81+
->getMock();
82+
$this->subscribersCollection = $this->getMockBuilder('\Magento\Newsletter\Model\Resource\Subscriber\Collection')
83+
->disableOriginalConstructor()
84+
->getMock();
85+
$this->resource = $this->getMockBuilder('\Magento\Newsletter\Model\Resource\Queue')
86+
->disableOriginalConstructor()
87+
->getMock();
88+
$this->subscribersCollectionFactory = $this->getMockBuilder(
89+
'\Magento\Newsletter\Model\Resource\Subscriber\CollectionFactory'
90+
)
91+
->disableOriginalConstructor()
92+
->setMethods(['create'])
93+
->getMock();
94+
$this->subscribersCollectionFactory->expects($this->any())->method('create')->willReturn(
95+
$this->subscribersCollection
96+
);
97+
98+
$this->objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
99+
100+
$this->queue = $this->objectManager->getObject(
101+
'\Magento\Newsletter\Model\Queue',
102+
[
103+
'templateFilter' => $this->templateFilter,
104+
'date' => $this->date,
105+
'templateFactory' => $this->templateFactory,
106+
'problemFactory' => $this->problemFactory,
107+
'subscriberCollectionFactory' => $this->subscribersCollectionFactory,
108+
'transportBuilder' => $this->transportBuilder,
109+
'resource' => $this->resource
110+
]
111+
);
112+
}
113+
114+
public function testSendPerSubscriber1()
115+
{
116+
$this->queue->setQueueStatus(2);
117+
$this->queue->setQueueStartAt(1);
118+
119+
$this->assertEquals($this->queue, $this->queue->sendPerSubscriber());
120+
}
121+
122+
public function testSendPerSubscriberZeroSize()
123+
{
124+
$this->queue->setQueueStatus(1);
125+
$this->queue->setQueueStartAt(1);
126+
$this->subscribersCollection->expects($this->once())->method('getQueueJoinedFlag')->willReturn(false);
127+
$this->subscribersCollection->expects($this->once())->method('useQueue')->with($this->queue)->willReturnSelf();
128+
$this->subscribersCollection->expects($this->once())->method('getSize')->willReturn(0);
129+
$this->date->expects($this->once())->method('gmtDate')->willReturn('any_date');
130+
131+
$this->assertEquals($this->queue, $this->queue->sendPerSubscriber());
132+
}
133+
134+
public function testSendPerSubscriber2()
135+
{
136+
$this->queue->setQueueStatus(1);
137+
$this->queue->setQueueStartAt(1);
138+
$collection = $this->getMockBuilder('\Magento\Framework\Data\Collection')
139+
->disableOriginalConstructor()
140+
->setMethods(['getItems'])
141+
->getMock();
142+
$item = $this->getMockBuilder('\Magento\Newsletter\Model\Subscriber')
143+
->disableOriginalConstructor()
144+
->setMethods(['getStoreId', 'getSubscriberEmail', 'getSubscriberFullName', 'received'])
145+
->getMock();
146+
$transport = $this->getMock('\Magento\Framework\Mail\TransportInterface');
147+
$this->subscribersCollection->expects($this->once())->method('getQueueJoinedFlag')->willReturn(false);
148+
$this->subscribersCollection->expects($this->once())->method('useQueue')->with($this->queue)->willReturnSelf();
149+
$this->subscribersCollection->expects($this->once())->method('getSize')->willReturn(5);
150+
$this->subscribersCollection->expects($this->once())->method('useOnlyUnsent')->willReturnSelf();
151+
$this->subscribersCollection->expects($this->once())->method('showCustomerInfo')->willReturnSelf();
152+
$this->subscribersCollection->expects($this->once())->method('setPageSize')->willReturnSelf();
153+
$this->subscribersCollection->expects($this->once())->method('setCurPage')->willReturnSelf();
154+
$this->subscribersCollection->expects($this->once())->method('load')->willReturn($collection);
155+
$this->transportBuilder->expects($this->once())->method('setTemplateData')->willReturnSelf();
156+
$collection->expects($this->atLeastOnce())->method('getItems')->willReturn([$item]);
157+
$item->expects($this->once())->method('getStoreId')->willReturn('store_id');
158+
$item->expects($this->once())->method('getSubscriberEmail')->willReturn('email');
159+
$item->expects($this->once())->method('getSubscriberFullName')->willReturn('full_name');
160+
$this->transportBuilder->expects($this->once())->method('setTemplateOptions')->willReturnSelf();
161+
$this->transportBuilder->expects($this->once())->method('setTemplateVars')->willReturnSelf();
162+
$this->transportBuilder->expects($this->once())->method('setFrom')->willReturnSelf();
163+
$this->transportBuilder->expects($this->once())->method('addTo')->willReturnSelf();
164+
$this->transportBuilder->expects($this->once())->method('getTransport')->willReturn($transport);
165+
$item->expects($this->once())->method('received')->with($this->queue)->willReturnSelf();
166+
167+
$this->assertEquals($this->queue, $this->queue->sendPerSubscriber());
168+
}
169+
170+
public function testGetDataForSave()
171+
{
172+
$result = [
173+
'template_id' => 'id',
174+
'queue_status' => 'status',
175+
'queue_start_at' => 'start_at',
176+
'queue_finish_at' => 'finish_at'
177+
];
178+
$this->queue->setTemplateId('id');
179+
$this->queue->setQueueStatus('status');
180+
$this->queue->setQueueStartAt('start_at');
181+
$this->queue->setQueueFinishAt('finish_at');
182+
183+
$this->assertEquals($result, $this->queue->getDataForSave());
184+
}
185+
186+
public function testGetTemplate()
187+
{
188+
$template = $this->getMockBuilder('\Magento\Newsletter\Model\Template')
189+
->disableOriginalConstructor()
190+
->getMock();
191+
$this->queue->setTemplateId(2);
192+
$this->templateFactory->expects($this->once())->method('create')->willReturn($template);
193+
$template->expects($this->once())->method('load')->with(2)->willReturnSelf();
194+
195+
$this->assertEquals($template, $this->queue->getTemplate());
196+
}
197+
198+
public function testGetStores()
199+
{
200+
$stores = ['store'];
201+
$this->resource->expects($this->once())->method('getStores')->willReturn($stores);
202+
203+
$this->assertEquals($stores, $this->queue->getStores());
204+
}
205+
}

0 commit comments

Comments
 (0)