Пример #1
0
 /**
  * @magentoDataFixture Magento/Customer/_files/customer.php
  * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  * @dataProvider customerFunctionDataProvider
  *
  * @param bool isCustomerIdUsed
  */
 public function testSend($isCustomerIdUsed)
 {
     \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea(\Magento\Framework\App\Area::AREA_FRONTEND);
     $this->_emailModel = $this->_objectManager->create('Magento\\ProductAlert\\Model\\Email');
     /** @var \Magento\Store\Model\Website $website */
     $website = $this->_objectManager->create('Magento\\Store\\Model\\Website');
     $website->load(1);
     $this->_emailModel->setWebsite($website);
     /** @var \Magento\Customer\Api\Data\CustomerInterface $customer */
     $customerRepository = $this->_objectManager->create('Magento\\Customer\\Api\\CustomerRepositoryInterface');
     $customer = $customerRepository->getById(1);
     if ($isCustomerIdUsed) {
         $this->_emailModel->setCustomerId(1);
     } else {
         $this->_emailModel->setCustomerData($customer);
     }
     /** @var \Magento\Catalog\Model\Product $product */
     $product = $this->_objectManager->create('Magento\\Catalog\\Model\\Product');
     $product->load(1);
     $this->_emailModel->addPriceProduct($product);
     $this->_emailModel->send();
     /** @var \Magento\TestFramework\Mail\Template\TransportBuilderMock $transportBuilder */
     $transportBuilder = $this->_objectManager->get('Magento\\TestFramework\\Mail\\Template\\TransportBuilderMock');
     $this->assertContains('John Smith,', $transportBuilder->getSentMessage()->getBodyHtml()->getRawContent());
 }
Пример #2
0
 /**
  * @magentoDataFixture Magento/Customer/_files/customer.php
  * @magentoDataFixture Magento/Catalog/_files/product_simple.php
  */
 public function testSend()
 {
     $this->_objectManager->configure(['Magento\\ProductAlert\\Model\\Email' => ['arguments' => ['transportBuilder' => ['instance' => 'Magento\\TestFramework\\Mail\\Template\\TransportBuilderMock']]], 'preferences' => ['Magento\\Framework\\Mail\\TransportInterface' => 'Magento\\TestFramework\\Mail\\TransportInterfaceMock']]);
     \Magento\TestFramework\Helper\Bootstrap::getInstance()->loadArea(\Magento\Framework\App\Area::AREA_FRONTEND);
     $this->_emailModel = $this->_objectManager->create('Magento\\ProductAlert\\Model\\Email');
     /** @var \Magento\Store\Model\Website $website */
     $website = $this->_objectManager->create('Magento\\Store\\Model\\Website');
     $website->load(1);
     $this->_emailModel->setWebsite($website);
     /** @var \Magento\Customer\Service\V1\Data\Customer $customer */
     $customer = $this->_customerAccountService->getCustomer(1);
     $this->_emailModel->setCustomerData($customer);
     /** @var \Magento\Catalog\Model\Product $product */
     $product = $this->_objectManager->create('Magento\\Catalog\\Model\\Product');
     $product->load(1);
     $this->_emailModel->addPriceProduct($product);
     $this->_emailModel->send();
     /** @var \Magento\TestFramework\Mail\Template\TransportBuilderMock $transportBuilder */
     $transportBuilder = $this->_objectManager->get('Magento\\TestFramework\\Mail\\Template\\TransportBuilderMock');
     $this->assertStringMatchesFormat('%AHello ' . $this->_customerViewHelper->getCustomerName($customer) . '%A', $transportBuilder->getSentMessage()->getBodyHtml()->getContent());
 }
Пример #3
0
 /**
  * Process stock emails
  *
  * @param \Magento\ProductAlert\Model\Email $email
  * @return $this
  */
 protected function _processStock(\Magento\ProductAlert\Model\Email $email)
 {
     $email->setType('stock');
     foreach ($this->_getWebsites() as $website) {
         /* @var $website \Magento\Store\Model\Website */
         if (!$website->getDefaultGroup() || !$website->getDefaultGroup()->getDefaultStore()) {
             continue;
         }
         if (!$this->_scopeConfig->getValue(self::XML_PATH_STOCK_ALLOW, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $website->getDefaultGroup()->getDefaultStore()->getId())) {
             continue;
         }
         try {
             $collection = $this->_stockColFactory->create()->addWebsiteFilter($website->getId())->addStatusFilter(0)->setCustomerOrder();
         } catch (\Exception $e) {
             $this->_errors[] = $e->getMessage();
             return $this;
         }
         $previousCustomer = null;
         $email->setWebsite($website);
         foreach ($collection as $alert) {
             try {
                 if (!$previousCustomer || $previousCustomer->getId() != $alert->getCustomerId()) {
                     $customer = $this->_customerAccountService->getCustomer($alert->getCustomerId());
                     if ($previousCustomer) {
                         $email->send();
                     }
                     if (!$customer) {
                         continue;
                     }
                     $previousCustomer = $customer;
                     $email->clean();
                     $email->setCustomerData($customer);
                 } else {
                     $customer = $previousCustomer;
                 }
                 $product = $this->_productFactory->create()->setStoreId($website->getDefaultStore()->getId())->load($alert->getProductId());
                 /* @var $product \Magento\Catalog\Model\Product */
                 if (!$product) {
                     continue;
                 }
                 $product->setCustomerGroupId($customer->getGroupId());
                 if ($product->isSalable()) {
                     $email->addStockProduct($product);
                     $alert->setSendDate($this->_dateFactory->create()->gmtDate());
                     $alert->setSendCount($alert->getSendCount() + 1);
                     $alert->setStatus(1);
                     $alert->save();
                 }
             } catch (\Exception $e) {
                 $this->_errors[] = $e->getMessage();
             }
         }
         if ($previousCustomer) {
             try {
                 $email->send();
             } catch (\Exception $e) {
                 $this->_errors[] = $e->getMessage();
             }
         }
     }
     return $this;
 }