/** * @dataProvider getUrlDataProvider * @covers \Magento\Catalog\Model\Product\Url::getUrl * @covers \Magento\Catalog\Model\Product\Url::getUrlInStore * @covers \Magento\Catalog\Model\Product\Url::getProductUrl * * @param $getUrlMethod * @param $routePath * @param $requestPathProduct * @param $storeId * @param $categoryId * @param $routeParams * @param $routeParamsUrl * @param $productId * @param $productUrlKey * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ public function testGetUrl($getUrlMethod, $routePath, $requestPathProduct, $storeId, $categoryId, $routeParams, $routeParamsUrl, $productId, $productUrlKey) { $product = $this->getMockBuilder('Magento\\Catalog\\Model\\Product')->disableOriginalConstructor()->setMethods(['getStoreId', 'getEntityId', 'getId', 'getUrlKey', 'setRequestPath', 'hasUrlDataObject', 'getRequestPath', 'getCategoryId', 'getDoNotUseCategoryId', '__wakeup'])->getMock(); $product->expects($this->any())->method('getStoreId')->will($this->returnValue($storeId)); $product->expects($this->any())->method('getCategoryId')->will($this->returnValue($categoryId)); $product->expects($this->any())->method('getRequestPath')->will($this->returnValue($requestPathProduct)); $product->expects($this->any())->method('setRequestPath')->with(false)->will($this->returnSelf()); $product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $product->expects($this->any())->method('getUrlKey')->will($this->returnValue($productUrlKey)); $this->url->expects($this->any())->method('setScope')->with($storeId)->will($this->returnSelf()); $this->url->expects($this->any())->method('getUrl')->with($routePath, $routeParamsUrl)->will($this->returnValue($requestPathProduct)); $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue(false)); switch ($getUrlMethod) { case 'getUrl': $this->assertEquals($requestPathProduct, $this->model->getUrl($product, $routeParams)); break; case 'getUrlInStore': $this->assertEquals($requestPathProduct, $this->model->getUrlInStore($product, $routeParams)); break; case 'getProductUrl': $this->assertEquals($requestPathProduct, $this->model->getProductUrl($product, true)); $this->sidResolver->expects($this->once())->method('getUseSessionInUrl')->will($this->returnValue(true)); $this->assertEquals($requestPathProduct, $this->model->getProductUrl($product, null)); break; } }
public function testGetHrefWithForProductWithCategoryIdParameter() { $storeId = 15; $this->block->setData('id_path', ProductUrlRewriteGenerator::ENTITY_TYPE . '/entity_id/category_id'); $store = $this->getMock('Magento\\Store\\Model\\Store', ['getId', '__wakeUp'], [], '', false); $store->expects($this->any())->method('getId')->will($this->returnValue($storeId)); $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($store)); $this->urlFinder->expects($this->once())->method('findOneByData')->with([UrlRewrite::ENTITY_ID => 'entity_id', UrlRewrite::ENTITY_TYPE => ProductUrlRewriteGenerator::ENTITY_TYPE, UrlRewrite::STORE_ID => $storeId, UrlRewrite::METADATA => ['category_id' => 'category_id']])->will($this->returnValue(false)); $this->block->getHref(); }
public function testMatch() { $this->storeManager->expects($this->any())->method('getStore')->will($this->returnValue($this->store)); $urlRewrite = $this->getMockBuilder('Magento\\UrlRewrite\\Service\\V1\\Data\\UrlRewrite')->disableOriginalConstructor()->getMock(); $urlRewrite->expects($this->any())->method('getRedirectType')->will($this->returnValue(0)); $urlRewrite->expects($this->any())->method('getTargetPath')->will($this->returnValue('target-path')); $this->urlFinder->expects($this->any())->method('findOneByData')->will($this->returnValue($urlRewrite)); $this->request->expects($this->once())->method('setPathInfo')->with('/target-path'); $this->actionFactory->expects($this->once())->method('create')->with('Magento\\Framework\\App\\Action\\Forward', ['request' => $this->request]); $this->router->match($this->request); }
public function testGenerationForCustomWithTargetPathGeneration() { $storeId = 12; $productId = 123; $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'generated-target-path.html'; $description = 'description'; $this->urlFinder->expects($this->once())->method('findAllByData')->will($this->returnValue($this->getCurrentRewritesMocks([[UrlRewrite::REQUEST_PATH => $requestPath, UrlRewrite::TARGET_PATH => 'custom-target-path.html', UrlRewrite::REDIRECT_TYPE => 'code', UrlRewrite::IS_AUTOGENERATED => 0, UrlRewrite::DESCRIPTION => $description, UrlRewrite::METADATA => []]]))); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->will($this->returnValue($targetPath)); $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $this->prepareUrlRewriteMock($storeId, $productId, $requestPath, $targetPath, 'code', [], $description); $this->assertEquals([$this->urlRewrite], $this->currentUrlRewritesRegenerator->generate($storeId, $this->product, $this->objectRegistry)); }
/** * Cover currentUrlRewritesRegenerate(). * * @cover \Magento\CatalogUrlRewrite\Model\Product\Plugin\Import::generateForCustom */ public function testCurrentUrlRewritesRegenerateForCustomWithTargetPathGeneration() { $productId = 'entity id value'; $storeId = 'store id value'; $products = [$productId => [$storeId => 'value']]; $this->setPropertyValue($this->import, 'products', $products); $requestPath = 'generate-for-custom-without-redirect-type.html'; $targetPath = 'generated-target-path.html'; $description = 'description'; $this->urlFinder->expects($this->once())->method('findAllByData')->will($this->returnValue($this->currentUrlRewritesRegeneratorGetCurrentRewritesMocks([[UrlRewrite::REQUEST_PATH => $requestPath, UrlRewrite::TARGET_PATH => 'custom-target-path.html', UrlRewrite::REDIRECT_TYPE => 'code', UrlRewrite::IS_AUTOGENERATED => 0, UrlRewrite::DESCRIPTION => $description, UrlRewrite::METADATA => [], UrlRewrite::ENTITY_ID => $productId, UrlRewrite::STORE_ID => $storeId]]))); $this->productUrlPathGenerator->expects($this->any())->method('getUrlPathWithSuffix')->will($this->returnValue($targetPath)); $this->product->expects($this->any())->method('getId')->will($this->returnValue($productId)); $this->currentUrlRewritesRegeneratorPrepareUrlRewriteMock($storeId, $productId, $requestPath, $targetPath, 'code', [], $description); $actualResult = $this->import->currentUrlRewritesRegenerate(); $this->assertEquals([[$this->urlRewrite]], $actualResult); }
/** * 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); }