Skip to content

Commit 6faa95a

Browse files
ENGCOM-8462: Ensure that url suffix resolvers return strings to match function declaration #30910
2 parents 4217826 + 1ac3123 commit 6faa95a

File tree

4 files changed

+348
-2
lines changed

4 files changed

+348
-2
lines changed

app/code/Magento/CatalogUrlRewriteGraphQl/Model/Resolver/CategoryUrlSuffix.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private function getCategoryUrlSuffix(int $storeId): string
7575
self::$xml_path_category_url_suffix,
7676
ScopeInterface::SCOPE_STORE,
7777
$storeId
78-
);
78+
) ?? '';
7979
}
8080
return $this->categoryUrlSuffix[$storeId];
8181
}

app/code/Magento/CatalogUrlRewriteGraphQl/Model/Resolver/ProductUrlSuffix.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private function getProductUrlSuffix(int $storeId): string
7575
self::$xml_path_product_url_suffix,
7676
ScopeInterface::SCOPE_STORE,
7777
$storeId
78-
);
78+
) ?? '';
7979
}
8080
return $this->productUrlSuffix[$storeId];
8181
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogUrlRewriteGraphQl\Test\Unit\Model\Resolver;
8+
9+
use Magento\CatalogUrlRewriteGraphQl\Model\Resolver\CategoryUrlSuffix;
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
use Magento\GraphQl\Model\Query\ContextExtensionInterface;
13+
use Magento\GraphQl\Model\Query\ContextInterface;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
use Magento\Framework\App\Config\ScopeConfigInterface;
18+
19+
/**
20+
* Test for \Magento\CatalogUrlRewriteGraphQl\Model\Resolver\CategoryUrlSuffix.
21+
*/
22+
class CategoryUrlSuffixTest extends TestCase
23+
{
24+
/**
25+
* @var ScopeConfigInterface|MockObject
26+
*/
27+
private $scopeConfigMock;
28+
29+
/**
30+
* @var ContextInterface|MockObject
31+
*/
32+
private $contextMock;
33+
34+
/**
35+
* @var ContextExtensionInterface|MockObject
36+
*/
37+
private $contextExtensionMock;
38+
39+
/**
40+
* @var StoreInterface|MockObject
41+
*/
42+
private $storeMock;
43+
44+
/**
45+
* @var Field|MockObject
46+
*/
47+
private $fieldMock;
48+
49+
/**
50+
* @var ResolveInfo|MockObject
51+
*/
52+
private $resolveInfoMock;
53+
54+
/**
55+
* @var CategoryUrlSuffix
56+
*/
57+
private $resolver;
58+
59+
/**
60+
* @inheritDoc
61+
*/
62+
protected function setUp(): void
63+
{
64+
$this->contextMock = $this->getMockBuilder(ContextInterface::class)
65+
->disableOriginalConstructor()
66+
->setMethods(
67+
[
68+
'getExtensionAttributes'
69+
]
70+
)
71+
->getMockForAbstractClass();
72+
73+
$this->contextExtensionMock = $this->getMockBuilder(ContextExtensionInterface::class)
74+
->setMethods(
75+
[
76+
'getStore'
77+
]
78+
)
79+
->getMockForAbstractClass();
80+
81+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
82+
->setMethods(
83+
[
84+
'getId'
85+
]
86+
)
87+
->getMockForAbstractClass();
88+
89+
$this->fieldMock = $this->getMockBuilder(Field::class)
90+
->disableOriginalConstructor()
91+
->getMock();
92+
93+
$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
94+
->disableOriginalConstructor()
95+
->getMock();
96+
97+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
98+
->getMockForAbstractClass();
99+
100+
$this->resolver = new CategoryUrlSuffix(
101+
$this->scopeConfigMock
102+
);
103+
}
104+
105+
/**
106+
* Verify that empty string is returned when config value is null
107+
*/
108+
public function testNullValue()
109+
{
110+
$this->scopeConfigMock->expects($this->once())
111+
->method('getValue')
112+
->willReturn(null);
113+
114+
$this->contextMock
115+
->expects($this->once())
116+
->method('getExtensionAttributes')
117+
->willReturn($this->contextExtensionMock);
118+
119+
$this->contextExtensionMock
120+
->expects($this->once())
121+
->method('getStore')
122+
->willReturn($this->storeMock);
123+
124+
$this->storeMock
125+
->expects($this->once())
126+
->method('getId')
127+
->willReturn(1);
128+
129+
$this->assertEquals(
130+
'',
131+
$this->resolver->resolve(
132+
$this->fieldMock,
133+
$this->contextMock,
134+
$this->resolveInfoMock
135+
)
136+
);
137+
}
138+
139+
/**
140+
* Verify that the configured value is returned
141+
*/
142+
public function testNonNullValue()
143+
{
144+
$value = 'html';
145+
$this->scopeConfigMock->expects($this->once())
146+
->method('getValue')
147+
->willReturn($value);
148+
149+
$this->contextMock
150+
->expects($this->once())
151+
->method('getExtensionAttributes')
152+
->willReturn($this->contextExtensionMock);
153+
154+
$this->contextExtensionMock
155+
->expects($this->once())
156+
->method('getStore')
157+
->willReturn($this->storeMock);
158+
159+
$this->storeMock
160+
->expects($this->once())
161+
->method('getId')
162+
->willReturn(1);
163+
164+
$this->assertEquals(
165+
$value,
166+
$this->resolver->resolve(
167+
$this->fieldMock,
168+
$this->contextMock,
169+
$this->resolveInfoMock
170+
)
171+
);
172+
}
173+
}
Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\CatalogUrlRewriteGraphQl\Test\Unit\Model\Resolver;
8+
9+
use Magento\CatalogUrlRewriteGraphQl\Model\Resolver\ProductUrlSuffix;
10+
use Magento\Framework\GraphQl\Config\Element\Field;
11+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
12+
use Magento\GraphQl\Model\Query\ContextExtensionInterface;
13+
use Magento\GraphQl\Model\Query\ContextInterface;
14+
use Magento\Store\Api\Data\StoreInterface;
15+
use PHPUnit\Framework\MockObject\MockObject;
16+
use PHPUnit\Framework\TestCase;
17+
use Magento\Framework\App\Config\ScopeConfigInterface;
18+
19+
/**
20+
* Test for \Magento\CatalogUrlRewriteGraphQl\Model\Resolver\ProductUrlSuffix.
21+
*/
22+
class ProductUrlSuffixTest extends TestCase
23+
{
24+
/**
25+
* @var ScopeConfigInterface|MockObject
26+
*/
27+
private $scopeConfigMock;
28+
29+
/**
30+
* @var ContextInterface|MockObject
31+
*/
32+
private $contextMock;
33+
34+
/**
35+
* @var ContextExtensionInterface|MockObject
36+
*/
37+
private $contextExtensionMock;
38+
39+
/**
40+
* @var StoreInterface|MockObject
41+
*/
42+
private $storeMock;
43+
44+
/**
45+
* @var Field|MockObject
46+
*/
47+
private $fieldMock;
48+
49+
/**
50+
* @var ResolveInfo|MockObject
51+
*/
52+
private $resolveInfoMock;
53+
54+
/**
55+
* @var ProductUrlSuffix
56+
*/
57+
private $resolver;
58+
59+
/**
60+
* @inheritDoc
61+
*/
62+
protected function setUp(): void
63+
{
64+
$this->contextMock = $this->getMockBuilder(ContextInterface::class)
65+
->disableOriginalConstructor()
66+
->setMethods(
67+
[
68+
'getExtensionAttributes'
69+
]
70+
)
71+
->getMockForAbstractClass();
72+
73+
$this->contextExtensionMock = $this->getMockBuilder(ContextExtensionInterface::class)
74+
->setMethods(
75+
[
76+
'getStore'
77+
]
78+
)
79+
->getMockForAbstractClass();
80+
81+
$this->storeMock = $this->getMockBuilder(StoreInterface::class)
82+
->setMethods(
83+
[
84+
'getId'
85+
]
86+
)
87+
->getMockForAbstractClass();
88+
89+
$this->fieldMock = $this->getMockBuilder(Field::class)
90+
->disableOriginalConstructor()
91+
->getMock();
92+
93+
$this->resolveInfoMock = $this->getMockBuilder(ResolveInfo::class)
94+
->disableOriginalConstructor()
95+
->getMock();
96+
97+
$this->scopeConfigMock = $this->getMockBuilder(ScopeConfigInterface::class)
98+
->getMockForAbstractClass();
99+
100+
$this->resolver = new ProductUrlSuffix(
101+
$this->scopeConfigMock
102+
);
103+
}
104+
105+
/**
106+
* Verify that empty string is returned when config value is null
107+
*/
108+
public function testNullValue()
109+
{
110+
$this->scopeConfigMock->expects($this->once())
111+
->method('getValue')
112+
->willReturn(null);
113+
114+
$this->contextMock
115+
->expects($this->once())
116+
->method('getExtensionAttributes')
117+
->willReturn($this->contextExtensionMock);
118+
119+
$this->contextExtensionMock
120+
->expects($this->once())
121+
->method('getStore')
122+
->willReturn($this->storeMock);
123+
124+
$this->storeMock
125+
->expects($this->once())
126+
->method('getId')
127+
->willReturn(1);
128+
129+
$this->assertEquals(
130+
'',
131+
$this->resolver->resolve(
132+
$this->fieldMock,
133+
$this->contextMock,
134+
$this->resolveInfoMock
135+
)
136+
);
137+
}
138+
139+
/**
140+
* Verify that the configured value is returned
141+
*/
142+
public function testNonNullValue()
143+
{
144+
$value = 'html';
145+
$this->scopeConfigMock->expects($this->once())
146+
->method('getValue')
147+
->willReturn($value);
148+
149+
$this->contextMock
150+
->expects($this->once())
151+
->method('getExtensionAttributes')
152+
->willReturn($this->contextExtensionMock);
153+
154+
$this->contextExtensionMock
155+
->expects($this->once())
156+
->method('getStore')
157+
->willReturn($this->storeMock);
158+
159+
$this->storeMock
160+
->expects($this->once())
161+
->method('getId')
162+
->willReturn(1);
163+
164+
$this->assertEquals(
165+
$value,
166+
$this->resolver->resolve(
167+
$this->fieldMock,
168+
$this->contextMock,
169+
$this->resolveInfoMock
170+
)
171+
);
172+
}
173+
}

0 commit comments

Comments
 (0)