/**
  * @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('John', $subscriber->getFirstname(), $subscriber->getSubscriberEmail());
     $this->assertEquals('Smith', $subscriber->getLastname(), $subscriber->getSubscriberEmail());
     $subscriber = array_shift($subscribers);
     $this->assertNull($subscriber->getFirstname(), $subscriber->getSubscriberEmail());
     $this->assertNull($subscriber->getLastname(), $subscriber->getSubscriberEmail());
 }
Example #2
0
 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());
 }
Example #3
0
 /**
  * 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\Exception\MailException $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;
 }
Example #4
0
 /**
  * Links queue to store
  *
  * @param ModelQueue $queue
  * @return $this
  */
 public function setStores(ModelQueue $queue)
 {
     $connection = $this->getConnection();
     $connection->delete($this->getTable('newsletter_queue_store_link'), ['queue_id = ?' => $queue->getId()]);
     $stores = $queue->getStores();
     if (!is_array($stores)) {
         $stores = [];
     }
     foreach ($stores as $storeId) {
         $data = [];
         $data['store_id'] = $storeId;
         $data['queue_id'] = $queue->getId();
         $connection->insert($this->getTable('newsletter_queue_store_link'), $data);
     }
     $this->removeSubscribersFromQueue($queue);
     if (count($stores) == 0) {
         return $this;
     }
     $subscribers = $this->_subscriberCollection->addFieldToFilter('store_id', ['in' => $stores])->useOnlySubscribed()->load();
     $subscriberIds = [];
     foreach ($subscribers as $subscriber) {
         $subscriberIds[] = $subscriber->getId();
     }
     if (count($subscriberIds) > 0) {
         $this->addSubscribersToQueue($queue, $subscriberIds);
     }
     return $this;
 }
Example #5
0
 /**
  * Sets flag for customer info loading on load
  *
  * @return $this
  */
 protected function _initSelect()
 {
     parent::_initSelect();
     $this->showCustomerInfo(true)->addSubscriberTypeField()->showStoreInfo();
     return $this;
 }