Skip to content
This repository was archived by the owner on Dec 19, 2019. It is now read-only.

Return full canonical product URL within product object #7

Merged
merged 3 commits into from
May 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Product;

use Magento\Catalog\Model\Product;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;

/**
* Resolve data for product canonical URL
*/
class CanonicalUrl implements ResolverInterface
{
/**
* @var ValueFactory
*/
private $valueFactory;

/**
* @param ValueFactory $valueFactory
*/
public function __construct(
ValueFactory $valueFactory
) {
$this->valueFactory = $valueFactory;
}

/**
* {@inheritdoc}
*/
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
): Value {
if (!isset($value['model'])) {
$result = function () {
return null;
};
return $this->valueFactory->create($result);
}

/* @var $product Product */
$product = $value['model'];
$url = $product->getUrlModel()->getUrl($product, ['_ignore_category' => true]);
$result = function () use ($url) {
return $url;
};

return $this->valueFactory->create($result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Test\Unit\Model\Resolver\Product;

use Magento\CatalogGraphQl\Model\Resolver\Product\CanonicalUrl;
use Magento\CatalogUrlRewrite\Model\ProductUrlPathGenerator;
use Magento\Framework\GraphQl\Query\Resolver\Value;
use Magento\Framework\GraphQl\Query\Resolver\ValueFactory;
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
use Magento\Store\Model\StoreManagerInterface;
use PHPUnit\Framework\TestCase;

class CanonicalUrlTest extends TestCase
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI, unit tests are optional and should be added only when it makes sense (e.g. when you can test method API without mocks). Since the test is already completed, we can keep it.

{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var CanonicalUrl
*/
private $subject;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $mockValueFactory;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $mockStoreManager;

public function testReturnsNullWhenNoProductAvailable()
{
$mockField = $this->getMockBuilder(\Magento\Framework\GraphQl\Config\Element\Field::class)
->disableOriginalConstructor()
->getMock();
$mockInfo = $this->getMockBuilder(\Magento\Framework\GraphQl\Schema\Type\ResolveInfo::class)
->disableOriginalConstructor()
->getMock();

$this->mockValueFactory->method('create')->with(
$this->callback(
function ($param) {
return $param() === null;
}
)
);

$this->subject->resolve($mockField, '', $mockInfo, [], []);
}

protected function setUp()
{
parent::setUp();
$this->objectManager = new ObjectManager($this);
$this->mockStoreManager = $this->getMockBuilder(StoreManagerInterface::class)->getMock();
$this->mockValueFactory = $this->getMockBuilder(ValueFactory::class)
->disableOriginalConstructor()
->getMock();

$this->mockValueFactory->method('create')->willReturn(
$this->objectManager->getObject(
Value::class,
['callback' => function () {
return '';
}]
)
);

$mockProductUrlPathGenerator = $this->getMockBuilder(ProductUrlPathGenerator::class)
->disableOriginalConstructor()
->getMock();
$mockProductUrlPathGenerator->method('getUrlPathWithSuffix')->willReturn('product_url.html');

$this->subject = $this->objectManager->getObject(
CanonicalUrl::class,
[
'valueFactory' => $this->mockValueFactory,
'storeManager' => $this->mockStoreManager,
'productUrlPathGenerator' => $mockProductUrlPathGenerator
]
);
}
}
1 change: 1 addition & 0 deletions app/code/Magento/CatalogGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ interface ProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\
gift_message_available: String @doc(description: "Indicates whether a gift message is available")
manufacturer: Int @doc(description: "A number representing the product's manufacturer")
categories: [CategoryInterface] @doc(description: "The categories assigned to a product") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Category")
canonical_url: String @doc(description: "Canonical URL") @resolver(class: "Magento\\CatalogGraphQl\\Model\\Resolver\\Product\\CanonicalUrl")
}

interface PhysicalProductInterface @typeResolver(class: "Magento\\CatalogGraphQl\\Model\\ProductInterfaceTypeResolverComposite") @doc(description: "PhysicalProductInterface contains attributes specific to tangible products") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public function testQueryAllFieldsSimpleProduct()
updated_at
url_key
url_path
canonical_url
websites { id name code sort_order default_group_id is_default }
... on PhysicalProductInterface {
weight
Expand Down Expand Up @@ -272,6 +273,11 @@ public function testQueryAllFieldsSimpleProduct()
'Filter category',
$responseObject->getData('products/items/0/categories/2/name')
);
$storeManager = ObjectManager::getInstance()->get(\Magento\Store\Model\StoreManagerInterface::class);
self::assertEquals(
$storeManager->getStore()->getBaseUrl() . 'simple-product.html',
$responseObject->getData('products/items/0/canonical_url')
);
}

/**
Expand Down