Skip to content

Commit 75b7014

Browse files
committed
IBX-9266: Add unit tests
1 parent 0533950 commit 75b7014

File tree

4 files changed

+75
-30
lines changed

4 files changed

+75
-30
lines changed

phpstan-baseline.neon

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,30 +2556,12 @@ parameters:
25562556
count: 1
25572557
path: tests/lib/RichText/Converter/LinkTest.php
25582558

2559-
-
2560-
message: '#^Parameter \#1 \$locationService of class Ibexa\\FieldTypeRichText\\RichText\\Converter\\Link constructor expects Ibexa\\Contracts\\Core\\Repository\\LocationService, PHPUnit\\Framework\\MockObject\\MockObject given\.$#'
2561-
identifier: argument.type
2562-
count: 5
2563-
path: tests/lib/RichText/Converter/LinkTest.php
2564-
2565-
-
2566-
message: '#^Parameter \#2 \$contentService of class Ibexa\\FieldTypeRichText\\RichText\\Converter\\Link constructor expects Ibexa\\Contracts\\Core\\Repository\\ContentService, PHPUnit\\Framework\\MockObject\\MockObject given\.$#'
2567-
identifier: argument.type
2568-
count: 5
2569-
path: tests/lib/RichText/Converter/LinkTest.php
2570-
25712559
-
25722560
message: '#^Parameter \#2 \$function of class Ibexa\\Core\\Base\\Exceptions\\UnauthorizedException constructor expects string, int given\.$#'
25732561
identifier: argument.type
25742562
count: 4
25752563
path: tests/lib/RichText/Converter/LinkTest.php
25762564

2577-
-
2578-
message: '#^Parameter \#3 \$router of class Ibexa\\FieldTypeRichText\\RichText\\Converter\\Link constructor expects Symfony\\Component\\Routing\\RouterInterface, PHPUnit\\Framework\\MockObject\\MockObject given\.$#'
2579-
identifier: argument.type
2580-
count: 5
2581-
path: tests/lib/RichText/Converter/LinkTest.php
2582-
25832565
-
25842566
message: '#^Call to method expects\(\) on an unknown class Ibexa\\FieldTypeRichText\\RichText\\RendererInterface\.$#'
25852567
identifier: class.notFound

src/bundle/Resources/config/fieldtype_services.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ services:
4949
- '@ibexa.api.service.location'
5050
- '@ibexa.api.service.content'
5151
- '@router'
52-
- '@Ibexa\AdminUi\Siteaccess\NonAdminSiteaccessResolver'
5352
- '@?logger'
5453
tags:
5554
- {name: ibexa.field_type.richtext.converter.output.xhtml5, priority: 0}

src/lib/RichText/Converter/Link.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use DOMDocument;
1212
use DOMXPath;
13-
use Ibexa\AdminUi\Siteaccess\SiteaccessResolverInterface;
1413
use Ibexa\Contracts\Core\Repository\ContentService;
1514
use Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException as APINotFoundException;
1615
use Ibexa\Contracts\Core\Repository\Exceptions\UnauthorizedException as APIUnauthorizedException;
@@ -44,19 +43,15 @@ class Link implements Converter
4443
*/
4544
protected $logger;
4645

47-
private SiteaccessResolverInterface $siteAccessResolver;
48-
4946
public function __construct(
5047
LocationService $locationService,
5148
ContentService $contentService,
5249
RouterInterface $router,
53-
SiteaccessResolverInterface $siteAccessResolver,
5450
?LoggerInterface $logger = null
5551
) {
5652
$this->locationService = $locationService;
5753
$this->contentService = $contentService;
5854
$this->router = $router;
59-
$this->siteAccessResolver = $siteAccessResolver;
6055
$this->logger = $logger;
6156
}
6257

@@ -153,7 +148,7 @@ private function generateUrlAliasForLocation(
153148
?string $siteaccess
154149
): string {
155150
$params = ['location' => $location];
156-
if ($siteaccess !== null) {
151+
if (!empty($siteaccess)) {
157152
$params['siteaccess'] = $siteaccess;
158153
}
159154

tests/lib/RichText/Converter/LinkTest.php

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,23 +28,23 @@
2828
class LinkTest extends TestCase
2929
{
3030
/**
31-
* @return \PHPUnit\Framework\MockObject\MockObject
31+
* @return \PHPUnit\Framework\MockObject\MockObject&\Ibexa\Core\Repository\ContentService
3232
*/
3333
protected function getMockContentService()
3434
{
3535
return $this->createMock(ContentService::class);
3636
}
3737

3838
/**
39-
* @return \PHPUnit\Framework\MockObject\MockObject
39+
* @return \PHPUnit\Framework\MockObject\MockObject&\Ibexa\Core\Repository\LocationService
4040
*/
4141
protected function getMockLocationService()
4242
{
4343
return $this->createMock(LocationService::class);
4444
}
4545

4646
/**
47-
* @return \PHPUnit\Framework\MockObject\MockObject
47+
* @return \PHPUnit\Framework\MockObject\MockObject&\Symfony\Component\Routing\RouterInterface
4848
*/
4949
protected function getMockRouter()
5050
{
@@ -144,7 +144,7 @@ public function testLink($input, $output)
144144
/**
145145
* @return array
146146
*/
147-
public function providerLocationLink()
147+
public function providerLocationLink(): array
148148
{
149149
return [
150150
[
@@ -197,6 +197,24 @@ public function providerLocationLink()
197197
<ezembed>
198198
<ezlink xlink:href="ezlocation://106#anchor" href_resolved="test#anchor"/>
199199
</ezembed>
200+
</section>',
201+
106,
202+
'test',
203+
],
204+
[
205+
'<?xml version="1.0" encoding="UTF-8"?>
206+
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ibexa.co/xmlns/dxp/docbook/xhtml" version="5.0-variant ezpublish-1.0">
207+
<title>Link example with empty site access</title>
208+
<ezembed>
209+
<ezlink xlink:href="ezlocation://106#anchor" xlink:siteaccess=""/>
210+
</ezembed>
211+
</section>',
212+
'<?xml version="1.0" encoding="UTF-8"?>
213+
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ibexa.co/xmlns/dxp/docbook/xhtml" version="5.0-variant ezpublish-1.0">
214+
<title>Link example with empty site access</title>
215+
<ezembed>
216+
<ezlink xlink:href="ezlocation://106#anchor" xlink:siteaccess="" href_resolved="test#anchor"/>
217+
</ezembed>
200218
</section>',
201219
106,
202220
'test',
@@ -240,10 +258,61 @@ public function testConvertLocationLink($input, $output, $locationId, $urlResolv
240258
$this->assertEquals($expectedOutputDocument, $outputDocument);
241259
}
242260

261+
/**
262+
* Test conversion of ezlocation://<id> links with the 'siteaccess' attribute.
263+
*/
264+
public function testConvertLocationLinkWithSiteAccess(): void
265+
{
266+
$inputDocument = new DOMDocument();
267+
$input = '<?xml version="1.0" encoding="UTF-8"?>
268+
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ibexa.co/xmlns/dxp/docbook/xhtml" version="5.0-variant ezpublish-1.0">
269+
<title>Link example with site access</title>
270+
<ezembed>
271+
<ezlink xlink:href="ezlocation://106#anchor" xlink:siteaccess="site"/>
272+
</ezembed>
273+
</section>';
274+
$output = '<?xml version="1.0" encoding="UTF-8"?>
275+
<section xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:ezxhtml="http://ibexa.co/xmlns/dxp/docbook/xhtml" version="5.0-variant ezpublish-1.0">
276+
<title>Link example with site access</title>
277+
<ezembed>
278+
<ezlink xlink:href="ezlocation://106#anchor" xlink:siteaccess="site" href_resolved="test#anchor"/>
279+
</ezembed>
280+
</section>';
281+
$inputDocument->loadXML($input);
282+
283+
$contentService = $this->getMockContentService();
284+
$locationService = $this->getMockLocationService();
285+
$router = $this->getMockRouter();
286+
287+
$location = $this->createMock(APILocation::class);
288+
289+
$locationService->expects($this->once())
290+
->method('loadLocation')
291+
->with($this->equalTo(106))
292+
->willReturn($location);
293+
294+
$router->expects($this->once())
295+
->method('generate')
296+
->with(UrlAliasRouter::URL_ALIAS_ROUTE_NAME, [
297+
'location' => $location,
298+
'siteaccess' => 'site',
299+
])
300+
->willReturn('test');
301+
302+
$converter = new Link($locationService, $contentService, $router);
303+
304+
$outputDocument = $converter->convert($inputDocument);
305+
306+
$expectedOutputDocument = new DOMDocument();
307+
$expectedOutputDocument->loadXML($output);
308+
309+
$this->assertEquals($expectedOutputDocument, $outputDocument);
310+
}
311+
243312
/**
244313
* @return array
245314
*/
246-
public function providerBadLocationLink()
315+
public function providerBadLocationLink(): array
247316
{
248317
return [
249318
[

0 commit comments

Comments
 (0)