/** * Test for afterImportData() * Covers afterImportData() + protected methods used inside except related to generateUrls() ones. * generateUrls will be covered separately. * * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::afterImportData * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::_populateForUrlGeneration * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::isGlobalScope * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::populateGlobalProduct * @covers \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::addProductToImport * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testAfterImportData() { $newSku = ['entity_id' => 'value']; $websiteId = 'websiteId value'; $productsCount = count($this->products); $websiteMock = $this->getMock('\\Magento\\Store\\Model\\Website', ['getStoreIds'], [], '', false); $storeIds = [1, Store::DEFAULT_STORE_ID]; $websiteMock->expects($this->once())->method('getStoreIds')->willReturn($storeIds); $this->storeManager->expects($this->once())->method('getWebsite')->with($websiteId)->willReturn($websiteMock); $this->importProduct->expects($this->exactly($productsCount))->method('getNewSku')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]])->willReturn($newSku); $this->importProduct->expects($this->exactly($productsCount))->method('getProductCategories')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]]); $getProductWebsitesCallsCount = $productsCount * 2; $this->importProduct->expects($this->exactly($getProductWebsitesCallsCount))->method('getProductWebsites')->willReturn([$newSku['entity_id'] => $websiteId]); $map = [[$this->products[0][ImportProduct::COL_STORE], $this->products[0][ImportProduct::COL_STORE]], [$this->products[1][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE]]]; $this->importProduct->expects($this->exactly(1))->method('getStoreIdByCode')->will($this->returnValueMap($map)); $product = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['getId', 'setId', 'getSku', 'setStoreId', 'getStoreId'], [], '', false); $product->expects($this->exactly($productsCount))->method('setId')->with($newSku['entity_id']); $product->expects($this->any())->method('getId')->willReturn($newSku['entity_id']); $product->expects($this->exactly($productsCount))->method('getSku')->will($this->onConsecutiveCalls($this->products[0]['sku'], $this->products[1]['sku'])); $product->expects($this->exactly($productsCount))->method('getStoreId')->will($this->onConsecutiveCalls($this->products[0][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE])); $product->expects($this->once())->method('setStoreId')->with($this->products[1][ImportProduct::COL_STORE]); $this->catalogProductFactory->expects($this->exactly($productsCount))->method('create')->willReturn($product); $this->connection->expects($this->exactly(4))->method('quoteInto')->withConsecutive(['(store_id = ?', $storeIds[0]], [' AND entity_id = ?)', $newSku['entity_id']]); $productUrls = ['url 1', 'url 2']; $importMock = $this->getImportMock(['generateUrls', 'canonicalUrlRewriteGenerate', 'categoriesUrlRewriteGenerate', 'currentUrlRewritesRegenerate', 'cleanOverriddenUrlKey']); $importMock->expects($this->once())->method('generateUrls')->willReturn($productUrls); $this->urlPersist->expects($this->once())->method('replace')->with($productUrls); $importMock->afterImportData($this->observer); }
/** * @return void */ public function testExecuteWithoutDataChanged() { $this->pageMock->expects($this->any())->method('dataHasChangedFor')->willReturnMap([['identifier', false], ['store_id', false]]); $this->pageMock->expects($this->never())->method('getId'); $this->cmsPageUrlRewriteGeneratorMock->expects($this->never())->method('generate'); $this->urlPersistMock->expects($this->never())->method('deleteByData'); $this->urlPersistMock->expects($this->never())->method('replace'); $this->observer->execute($this->eventObserverMock); }
/** * @param bool $isChangedUrlKey * @param bool $isChangedVisibility * @param bool $isChangedWebsites * @param bool $isChangedCategories * @param bool $visibilityResult * @param int $expectedDeleteCount * @param int $expectedReplaceCount * * @dataProvider testUrlKeyDataProvider */ public function testExecuteUrlKey($isChangedUrlKey, $isChangedVisibility, $isChangedWebsites, $isChangedCategories, $visibilityResult, $expectedDeleteCount, $expectedReplaceCount) { $this->product->expects($this->any())->method('getStoreId')->will($this->returnValue(12)); $this->product->expects($this->any())->method('dataHasChangedFor')->will($this->returnValueMap([['visibility', $isChangedVisibility], ['url_key', $isChangedUrlKey]])); $this->product->expects($this->any())->method('getIsChangedWebsites')->will($this->returnValue($isChangedWebsites)); $this->product->expects($this->any())->method('getIsChangedCategories')->will($this->returnValue($isChangedCategories)); $this->urlPersist->expects($this->exactly($expectedDeleteCount))->method('deleteByData')->with([UrlRewrite::ENTITY_ID => $this->product->getId(), UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::REDIRECT_TYPE => 0, UrlRewrite::STORE_ID => $this->product->getStoreId()]); $this->product->expects($this->any())->method('isVisibleInSiteVisibility')->will($this->returnValue($visibilityResult)); $this->urlPersist->expects($this->exactly($expectedReplaceCount))->method('replace')->with([3 => 'rewrite']); $this->model->execute($this->observer); }
/** * Test for afterImportData() * Covers afterImportData() + protected methods used inside * * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::afterImportData * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::_populateForUrlGeneration * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::isGlobalScope * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::populateGlobalProduct * @covers \Magento\CatalogUrlRewrite\Observer\AfterImportDataObserver::addProductToImport * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testAfterImportData() { $newSku = [['entity_id' => 'value'], ['entity_id' => 'value3']]; $websiteId = 'websiteId value'; $productsCount = count($this->products); $websiteMock = $this->getMock('\\Magento\\Store\\Model\\Website', ['getStoreIds'], [], '', false); $storeIds = [1, Store::DEFAULT_STORE_ID]; $websiteMock->expects($this->once())->method('getStoreIds')->willReturn($storeIds); $this->storeManager->expects($this->once())->method('getWebsite')->with($websiteId)->willReturn($websiteMock); $this->importProduct->expects($this->exactly($productsCount))->method('getNewSku')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]])->will($this->onConsecutiveCalls($newSku[0], $newSku[1])); $this->importProduct->expects($this->exactly($productsCount))->method('getProductCategories')->withConsecutive([$this->products[0][ImportProduct::COL_SKU]], [$this->products[1][ImportProduct::COL_SKU]])->willReturn([]); $getProductWebsitesCallsCount = $productsCount * 2; $this->importProduct->expects($this->exactly($getProductWebsitesCallsCount))->method('getProductWebsites')->willReturnOnConsecutiveCalls([$newSku[0]['entity_id'] => $websiteId], [$newSku[0]['entity_id'] => $websiteId], [$newSku[1]['entity_id'] => $websiteId], [$newSku[1]['entity_id'] => $websiteId]); $map = [[$this->products[0][ImportProduct::COL_STORE], $this->products[0][ImportProduct::COL_STORE]], [$this->products[1][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE]]]; $this->importProduct->expects($this->exactly(1))->method('getStoreIdByCode')->will($this->returnValueMap($map)); $product = $this->getMock('\\Magento\\Catalog\\Model\\Product', ['getId', 'setId', 'getSku', 'setStoreId', 'getStoreId'], [], '', false); $product->expects($this->exactly($productsCount))->method('setId')->withConsecutive([$newSku[0]['entity_id']], [$newSku[1]['entity_id']]); $product->expects($this->any())->method('getId')->willReturnOnConsecutiveCalls($newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[0]['entity_id'], $newSku[1]['entity_id'], $newSku[1]['entity_id'], $newSku[1]['entity_id']); $product->expects($this->exactly($productsCount))->method('getSku')->will($this->onConsecutiveCalls($this->products[0]['sku'], $this->products[1]['sku'])); $product->expects($this->exactly($productsCount))->method('getStoreId')->will($this->onConsecutiveCalls($this->products[0][ImportProduct::COL_STORE], $this->products[1][ImportProduct::COL_STORE])); $product->expects($this->exactly($productsCount))->method('setStoreId')->withConsecutive([$this->products[0][ImportProduct::COL_STORE]], [$this->products[1][ImportProduct::COL_STORE]]); $this->catalogProductFactory->expects($this->exactly($productsCount))->method('create')->willReturn($product); $this->connection->expects($this->exactly(4))->method('quoteInto')->withConsecutive(['(store_id = ?', $storeIds[0]], [' AND entity_id = ?)', $newSku[0]['entity_id']], ['(store_id = ?', $storeIds[0]], [' AND entity_id = ?)', $newSku[1]['entity_id']]); $this->connection->expects($this->once())->method('fetchAll')->willReturn([]); $this->select->expects($this->any())->method('from')->willReturnSelf(); $this->select->expects($this->any())->method('where')->willReturnSelf(); $this->urlFinder->expects($this->any())->method('findAllByData')->willReturn([]); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->willReturn('urlPathWithSuffix'); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPath')->willReturn('urlPath'); $this->productUrlPathGenerator->expects($this->any())->method('getCanonicalUrlPath')->willReturn('canonicalUrlPath'); $this->urlRewrite->expects($this->any())->method('setStoreId')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setEntityId')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setEntityType')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setRequestPath')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('setTargetPath')->willReturnSelf(); $this->urlRewrite->expects($this->any())->method('getTargetPath')->willReturn('targetPath'); $this->urlRewrite->expects($this->any())->method('getStoreId')->willReturnOnConsecutiveCalls(0, 'not global'); $this->urlRewriteFactory->expects($this->any())->method('create')->willReturn($this->urlRewrite); $productUrls = ['targetPath-0' => $this->urlRewrite, 'targetPath-not global' => $this->urlRewrite]; $this->urlPersist->expects($this->once())->method('replace')->with($productUrls); $this->import->execute($this->observer); }
public function testAroundDeleteNegative() { $this->cmsPageMock->expects($this->once())->method('isDeleted')->willReturn(false); $this->urlPersistMock->expects($this->never())->method('deleteByData'); $this->assertEquals('URL Rewrite Result', $this->pageObject->aroundDelete($this->cmsPageResourceMock, $this->closureMock, $this->cmsPageMock)); }