ContactSyncTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\Sync;
  3. /**
  4. * @magentoDBIsolation enabled
  5. * @magentoDataFixture Magento/Store/_files/website.php
  6. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  7. */
  8. class ContactSyncTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\ObjectManagerInterface
  12. */
  13. public $objectManager;
  14. /**
  15. * @var string
  16. */
  17. public $storeId;
  18. /**
  19. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Importer\Collection
  20. */
  21. public $importerCollection;
  22. /**
  23. * @return void
  24. */
  25. public function setup()
  26. {
  27. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  28. $this->importerCollection = $this->objectManager->create(
  29. \Dotdigitalgroup\Email\Model\ResourceModel\Importer\Collection::class
  30. );
  31. }
  32. /**
  33. * Run the preparation for the test with already existing data.
  34. * @return array
  35. */
  36. public function prep()
  37. {
  38. /** @var \Magento\Store\Model\Store $store */
  39. $store = $this->objectManager->create(\Magento\Store\Model\Store::class);
  40. $store->load(1);
  41. //load store with the code rathe then id ; $store->load('test', 'code')
  42. $helper = $this->getMockbuilder(\Dotdigitalgroup\Email\Helper\Data::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $helper->method('isEnabled')->willReturn(true);
  46. $helper->method('isCustomerSyncEnabled')->willReturn(true);
  47. $helper->method('getCustomerAddressBook')->willReturn('12345');
  48. $helper->method('getWebsites')->willReturn([$store->getWebsite()]);
  49. $helper->method('getApiUsername')->willReturn('apiuser-dummy@apiconnector.com');
  50. $helper->method('getApiPassword')->willReturn('dummypass');
  51. $helper->method('getWebsiteCustomerMappingDatafields')->willReturn($this->getHashedDataFields());
  52. $helper->method('getCustomAttributes')->willReturn([]);
  53. $apiconnectorContact = new \Dotdigitalgroup\Email\Model\Apiconnector\Contact(
  54. $this->objectManager->create(\Dotdigitalgroup\Email\Model\Apiconnector\CustomerFactory::class),
  55. $this->objectManager->create(\Dotdigitalgroup\Email\Helper\File::class),
  56. $helper,
  57. $this->objectManager->create(\Dotdigitalgroup\Email\Model\ResourceModel\Contact::class),
  58. $this->objectManager->create(\Dotdigitalgroup\Email\Model\Apiconnector\ContactImportQueueExport::class),
  59. $this->objectManager->create(\Dotdigitalgroup\Email\Model\ResourceModel\Contact\CollectionFactory::class)
  60. );
  61. return $apiconnectorContact->sync();
  62. }
  63. /**
  64. * Test the builk imports with the consent data.
  65. *
  66. * @magentoDataFixture Magento/Customer/_files/two_customers.php
  67. * @magentoConfigFixture default_store sync_settings/sync/customer_enabled 1
  68. * @magentoConfigFixture default_store connector_api_credentials/api/enabled 1
  69. */
  70. public function testContactBulkImportCreated()
  71. {
  72. $this->createSingleModifiedContact();
  73. $this->prep();
  74. $item = $this->importerCollection
  75. ->addFieldToFilter('import_type', \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_CONTACT)
  76. ->addFieldToFilter('import_mode', \Dotdigitalgroup\Email\Model\Importer::MODE_BULK)
  77. ->getFirstItem();
  78. $this->assertEquals(
  79. \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_CONTACT,
  80. $item->getImportType(),
  81. 'Item is not type of contact'
  82. );
  83. $this->assertEquals(
  84. \Dotdigitalgroup\Email\Model\Importer::MODE_BULK,
  85. $item->getImportMode(),
  86. 'Item is not in bulk mode'
  87. );
  88. }
  89. /**
  90. *
  91. *
  92. * @magentoDataFixture Magento/Customer/_files/two_customers.php
  93. * @magentoConfigFixture default_store sync_settings/sync/customer_enabled 1
  94. * @magentoConfigFixture default_store connector_api_credentials/api/enabled 1
  95. */
  96. public function testContatctWithConsentDataCreated()
  97. {
  98. $this->createSingleModifiedContact();
  99. $this->prep();
  100. $item = $this->importerCollection
  101. ->addFieldToFilter('import_type', \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_CONTACT)
  102. ->addFieldToFilter('import_mode', \Dotdigitalgroup\Email\Model\Importer::MODE_BULK)
  103. ->getFirstItem();
  104. $this->assertEquals(
  105. \Dotdigitalgroup\Email\Model\Importer::IMPORT_TYPE_CONTACT,
  106. $item->getImportType(),
  107. 'Item is not type of contact'
  108. );
  109. $this->assertEquals(
  110. \Dotdigitalgroup\Email\Model\Importer::MODE_BULK,
  111. $item->getImportMode(),
  112. 'Item is not in bulk mode'
  113. );
  114. }
  115. /**
  116. * Create customer and contact with dummy info.
  117. */
  118. public function createSingleModifiedContact()
  119. {
  120. $customerCollection = $this->objectManager->create(
  121. \Magento\Customer\Model\ResourceModel\Customer\Collection::class
  122. );
  123. $customer = $customerCollection->getFirstItem();
  124. $this->storeId = $customer->getStoreId();
  125. $emailContact = $this->objectManager->create(\Dotdigitalgroup\Email\Model\Contact::class)
  126. ->setCustomerId($customer->getId())
  127. ->setWebsiteId($customer->getWebsiteId())
  128. ->setStoreId($customer->getStoreId())
  129. ->setEmail($customer->getEmail())
  130. ->setEmailIMpoerted(null);
  131. $emailContact->save();
  132. }
  133. private function getHashedDataFields()
  134. {
  135. return [
  136. 'customer_id' => "CUSTOMER_ID",
  137. 'firstname' => "FIRSTNAME",
  138. 'lastname' => "LASTNAME",
  139. 'consent_text' => "CONSENTTEXT",
  140. 'consent_url' => "CONSENTURL",
  141. 'consent_datetime' => "CONSENTDATETIME",
  142. 'consent_ip' => "CONSENTIP",
  143. 'consent_user_agent' => "CONSENTUSERAGENT"
  144. ];
  145. }
  146. }