/** * @magentoDataFixture Magento/Newsletter/_files/subscribers.php */ public function testShowCustomerInfo() { $this->_collectionModel->showCustomerInfo()->load(); /** @var \Magento\Newsletter\Model\Subscriber[] $subscribers */ $subscribers = $this->_collectionModel->getItems(); $this->assertCount(2, $subscribers); $subscriber = array_shift($subscribers); $this->assertEquals('Firstname', $subscriber->getCustomerFirstname(), $subscriber->getSubscriberEmail()); $this->assertEquals('Lastname', $subscriber->getCustomerLastname(), $subscriber->getSubscriberEmail()); $subscriber = array_shift($subscribers); $this->assertNull($subscriber->getCustomerFirstname(), $subscriber->getSubscriberEmail()); $this->assertNull($subscriber->getCustomerLastname(), $subscriber->getSubscriberEmail()); }
/** * Send messages to subscribers for this queue * * @param int $count * @return $this * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function sendPerSubscriber($count = 20) { if ($this->getQueueStatus() != self::STATUS_SENDING && ($this->getQueueStatus() != self::STATUS_NEVER && $this->getQueueStartAt())) { return $this; } if (!$this->_subscribersCollection->getQueueJoinedFlag()) { $this->_subscribersCollection->useQueue($this); } if ($this->_subscribersCollection->getSize() == 0) { $this->_finishQueue(); return $this; } $collection = $this->_subscribersCollection->useOnlyUnsent()->showCustomerInfo()->setPageSize($count)->setCurPage(1)->load(); $this->_transportBuilder->setTemplateData(['template_subject' => $this->getNewsletterSubject(), 'template_text' => $this->getNewsletterText(), 'template_styles' => $this->getNewsletterStyles(), 'template_filter' => $this->_templateFilter, 'template_type' => self::TYPE_HTML]); /** @var \Magento\Newsletter\Model\Subscriber $item */ foreach ($collection->getItems() as $item) { $transport = $this->_transportBuilder->setTemplateOptions(['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $item->getStoreId()])->setTemplateVars(['subscriber' => $item])->setFrom(['name' => $this->getNewsletterSenderEmail(), 'email' => $this->getNewsletterSenderName()])->addTo($item->getSubscriberEmail(), $item->getSubscriberFullName())->getTransport(); try { $transport->sendMessage(); } catch (\Magento\Framework\Mail\Exception $e) { /** @var \Magento\Newsletter\Model\Problem $problem */ $problem = $this->_problemFactory->create(); $problem->addSubscriberData($item); $problem->addQueueData($this); $problem->addErrorData($e); $problem->save(); } $item->received($this); } if (count($collection->getItems()) < $count - 1 || count($collection->getItems()) == 0) { $this->_finishQueue(); } return $this; }
/** * Links queue to store * * @param ModelQueue $queue * @return $this */ public function setStores(ModelQueue $queue) { $adapter = $this->_getWriteAdapter(); $adapter->delete($this->getTable('newsletter_queue_store_link'), array('queue_id = ?' => $queue->getId())); $stores = $queue->getStores(); if (!is_array($stores)) { $stores = array(); } foreach ($stores as $storeId) { $data = array(); $data['store_id'] = $storeId; $data['queue_id'] = $queue->getId(); $adapter->insert($this->getTable('newsletter_queue_store_link'), $data); } $this->removeSubscribersFromQueue($queue); if (count($stores) == 0) { return $this; } $subscribers = $this->_subscriberCollection->addFieldToFilter('store_id', array('in' => $stores))->useOnlySubscribed()->load(); $subscriberIds = array(); foreach ($subscribers as $subscriber) { $subscriberIds[] = $subscriber->getId(); } if (count($subscriberIds) > 0) { $this->addSubscribersToQueue($queue, $subscriberIds); } return $this; }
public function testSendPerSubscriber2() { $this->queue->setQueueStatus(1); $this->queue->setQueueStartAt(1); $collection = $this->getMockBuilder('\\Magento\\Framework\\Data\\Collection')->disableOriginalConstructor()->setMethods(['getItems'])->getMock(); $item = $this->getMockBuilder('\\Magento\\Newsletter\\Model\\Subscriber')->disableOriginalConstructor()->setMethods(['getStoreId', 'getSubscriberEmail', 'getSubscriberFullName', 'received'])->getMock(); $transport = $this->getMock('\\Magento\\Framework\\Mail\\TransportInterface'); $this->subscribersCollection->expects($this->once())->method('getQueueJoinedFlag')->willReturn(false); $this->subscribersCollection->expects($this->once())->method('useQueue')->with($this->queue)->willReturnSelf(); $this->subscribersCollection->expects($this->once())->method('getSize')->willReturn(5); $this->subscribersCollection->expects($this->once())->method('useOnlyUnsent')->willReturnSelf(); $this->subscribersCollection->expects($this->once())->method('showCustomerInfo')->willReturnSelf(); $this->subscribersCollection->expects($this->once())->method('setPageSize')->willReturnSelf(); $this->subscribersCollection->expects($this->once())->method('setCurPage')->willReturnSelf(); $this->subscribersCollection->expects($this->once())->method('load')->willReturn($collection); $this->transportBuilder->expects($this->once())->method('setTemplateData')->willReturnSelf(); $collection->expects($this->atLeastOnce())->method('getItems')->willReturn([$item]); $item->expects($this->once())->method('getStoreId')->willReturn('store_id'); $item->expects($this->once())->method('getSubscriberEmail')->willReturn('email'); $item->expects($this->once())->method('getSubscriberFullName')->willReturn('full_name'); $this->transportBuilder->expects($this->once())->method('setTemplateOptions')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('setTemplateVars')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('setFrom')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('addTo')->willReturnSelf(); $this->transportBuilder->expects($this->once())->method('getTransport')->willReturn($transport); $item->expects($this->once())->method('received')->with($this->queue)->willReturnSelf(); $this->assertEquals($this->queue, $this->queue->sendPerSubscriber()); }
/** * Sets flag for customer info loading on load * * @return $this */ protected function _initSelect() { parent::_initSelect(); $this->showCustomerInfo(true)->addSubscriberTypeField()->showStoreInfo(); return $this; }