Skip to content

Commit fb1e2f4

Browse files
authored
Merge pull request #41 from ibexa/temp_1.3_to_4.3
Merge branch '1.3' of ezsystems/ezplatform-rest into 4.3
2 parents dad45bf + 1670be3 commit fb1e2f4

File tree

6 files changed

+140
-43
lines changed

6 files changed

+140
-43
lines changed

src/bundle/Resources/config/routing.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,14 @@ ibexa.rest.load_content_type_field_definition:
595595
requirements:
596596
contentTypeId: \d+
597597
fieldDefinitionId: \d+
598+
599+
ibexa.rest.load_content_type_field_definition_by_identifier:
600+
path: /content/types/{contentTypeId}/fieldDefinition/{fieldDefinitionIdentifier}
601+
controller: Ibexa\Rest\Server\Controller\ContentType::loadContentTypeFieldDefinitionByIdentifier
602+
methods: [GET]
603+
requirements:
604+
contentTypeId: \d+
605+
fieldDefinitionIdentifier: \w+
598606

599607
ibexa.rest.load_content_type_draft:
600608
path: /content/types/{contentTypeId}/draft

src/lib/Server/Controller/ContentType.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,38 @@ public function loadContentTypeFieldDefinition($contentTypeId, $fieldDefinitionI
560560
throw new Exceptions\NotFoundException("Field definition not found: '{$request->getPathInfo()}'.");
561561
}
562562

563+
/**
564+
* @throws \Ibexa\Contracts\Rest\Exceptions\NotFoundException
565+
* @throws \Ibexa\Contracts\Core\Repository\Exceptions\NotFoundException
566+
*/
567+
public function loadContentTypeFieldDefinitionByIdentifier(
568+
int $contentTypeId,
569+
string $fieldDefinitionIdentifier,
570+
Request $request
571+
): Values\RestFieldDefinition {
572+
$contentType = $this->contentTypeService->loadContentType($contentTypeId);
573+
$fieldDefinition = $contentType->getFieldDefinition($fieldDefinitionIdentifier);
574+
$path = $this->router->generate(
575+
'ibexa.rest.load_content_type_field_definition_by_identifier',
576+
[
577+
'contentTypeId' => $contentType->id,
578+
'fieldDefinitionIdentifier' => $fieldDefinitionIdentifier,
579+
]
580+
);
581+
582+
if ($fieldDefinition === null) {
583+
throw new Exceptions\NotFoundException(
584+
sprintf("Field definition not found: '%s'.", $request->getPathInfo())
585+
);
586+
}
587+
588+
return new Values\RestFieldDefinition(
589+
$contentType,
590+
$fieldDefinition,
591+
$path
592+
);
593+
}
594+
563595
/**
564596
* Loads field definitions for a given content type draft.
565597
*

src/lib/Server/Output/ValueObjectVisitor/RestFieldDefinition.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,17 +53,19 @@ public function visit(Visitor $visitor, Generator $generator, $data)
5353
$visitor->setHeader('Accept-Patch', $generator->getMediaType('FieldDefinitionUpdate'));
5454
}
5555

56-
$generator->startAttribute(
57-
'href',
58-
$this->router->generate(
56+
if ($data->path === null) {
57+
$href = $this->router->generate(
5958
"ibexa.rest.load_content_type{$urlTypeSuffix}_field_definition",
6059
[
6160
'contentTypeId' => $contentType->id,
6261
'fieldDefinitionId' => $fieldDefinition->id,
6362
]
64-
)
65-
);
66-
$generator->endAttribute('href');
63+
);
64+
} else {
65+
$href = $data->path;
66+
}
67+
68+
$generator->attribute('href', $href);
6769

6870
$generator->startValueElement('id', $fieldDefinition->id);
6971
$generator->endValueElement('id');

src/lib/Server/Values/RestFieldDefinition.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@ class RestFieldDefinition extends RestValue
3030
public $fieldDefinition;
3131

3232
/**
33-
* Construct.
34-
*
35-
* @param \Ibexa\Contracts\Core\Repository\Values\ContentType\ContentType $contentType
36-
* @param \Ibexa\Contracts\Core\Repository\Values\ContentType\FieldDefinition $fieldDefinition
33+
* Path which is used to fetch the list of field definitions.
3734
*/
38-
public function __construct(ContentType $contentType, FieldDefinition $fieldDefinition)
35+
public ?string $path;
36+
37+
public function __construct(ContentType $contentType, FieldDefinition $fieldDefinition, ?string $path = null)
3938
{
4039
$this->contentType = $contentType;
4140
$this->fieldDefinition = $fieldDefinition;
41+
$this->path = $path;
4242
}
4343
}
4444

tests/bundle/Functional/ContentTypeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,29 @@ public function testLoadContentTypeFieldDefinition(string $fieldDefinitionHref)
446446
self::assertHttpResponseCodeEquals($response, 200);
447447
}
448448

449+
/**
450+
* Covers GET /content/types/{contentTypeId}/fieldDefinition/{fieldDefinitionIdentifier}.
451+
*
452+
* @depends testCreateContentType
453+
*
454+
* @throws \Psr\Http\Client\ClientException
455+
*/
456+
public function testLoadContentTypeFieldDefinitionByIdentifier(string $contentTypeHref): void
457+
{
458+
$url = sprintf('%s/fieldDefinition/title', $contentTypeHref);
459+
460+
$response = $this->sendHttpRequest(
461+
$this->createHttpRequest('GET', $url, '', 'FieldDefinition+json')
462+
);
463+
464+
self::assertHttpResponseCodeEquals($response, 200);
465+
466+
$data = json_decode($response->getBody(), true);
467+
468+
self::assertEquals($url, $data['FieldDefinition']['_href']);
469+
self::assertEquals('title', $data['FieldDefinition']['identifier']);
470+
}
471+
449472
/**
450473
* @depends testAddContentTypeDraftFieldDefinition
451474
* Covers PATCH /content/types/<contentTypeId>/fieldDefinitions/<fieldDefinitionId>

tests/lib/Server/Output/ValueObjectVisitor/RestFieldDefinitionTest.php

Lines changed: 64 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,24 @@ public function setUp(): void
2121
$this->fieldTypeSerializerMock = $this->createMock(FieldTypeSerializer::class);
2222
}
2323

24-
/**
25-
* @return \DOMDocument
26-
*/
27-
public function testVisitRestFieldDefinition()
24+
public function testVisitRestFieldDefinition(): \DOMDocument
25+
{
26+
return $this->generateDomDocument();
27+
}
28+
29+
public function testVisitRestFieldDefinitionWithPath(): \DOMDocument
30+
{
31+
return $this->generateDomDocument('/content/types/contentTypeId/fieldDefinition/title');
32+
}
33+
34+
protected function generateDomDocument(?string $path = null): \DOMDocument
2835
{
2936
$visitor = $this->getVisitor();
3037
$generator = $this->getGenerator();
3138

3239
$generator->startDocument(null);
3340

34-
$restFieldDefinition = $this->getBasicRestFieldDefinition();
41+
$restFieldDefinition = $this->getBasicRestFieldDefinition($path);
3542

3643
$this->fieldTypeSerializerMock->expects($this->once())
3744
->method('serializeFieldDefaultValue')
@@ -43,14 +50,16 @@ public function testVisitRestFieldDefinition()
4350
)
4451
);
4552

46-
$this->addRouteExpectation(
47-
'ibexa.rest.load_content_type_field_definition',
48-
[
49-
'contentTypeId' => $restFieldDefinition->contentType->id,
50-
'fieldDefinitionId' => $restFieldDefinition->fieldDefinition->id,
51-
],
52-
"/content/types/{$restFieldDefinition->contentType->id}/fieldDefinitions/{$restFieldDefinition->fieldDefinition->id}"
53-
);
53+
if ($path === null) {
54+
$this->addRouteExpectation(
55+
'ibexa.rest.load_content_type_field_definition',
56+
[
57+
'contentTypeId' => $restFieldDefinition->contentType->id,
58+
'fieldDefinitionId' => $restFieldDefinition->fieldDefinition->id,
59+
],
60+
"/content/types/{$restFieldDefinition->contentType->id}/fieldDefinitions/{$restFieldDefinition->fieldDefinition->id}"
61+
);
62+
}
5463

5564
$visitor->visit(
5665
$this->getVisitorMock(),
@@ -68,7 +77,7 @@ public function testVisitRestFieldDefinition()
6877
return $dom;
6978
}
7079

71-
protected function getBasicRestFieldDefinition()
80+
protected function getBasicRestFieldDefinition(?string $path = null): Server\Values\RestFieldDefinition
7281
{
7382
return new Server\Values\RestFieldDefinition(
7483
new Values\ContentType\ContentType(
@@ -95,14 +104,40 @@ protected function getBasicRestFieldDefinition()
95104
'names' => ['eng-US' => 'Sindelfingen'],
96105
'descriptions' => ['eng-GB' => 'Bielefeld'],
97106
]
98-
)
107+
),
108+
$path
109+
);
110+
}
111+
112+
public function provideXpathAssertions(): array
113+
{
114+
$xpathAssertions = $this->getXpathAssertions();
115+
$xpathAssertions[] = '/FieldDefinition[@href="/content/types/contentTypeId/fieldDefinitions/fieldDefinitionId_23"]';
116+
117+
return $this->prepareXPathAssertions($xpathAssertions);
118+
}
119+
120+
public function provideXpathAssertionsPath(): array
121+
{
122+
$xpathAssertions = $this->getXpathAssertions();
123+
$xpathAssertions[] = '/FieldDefinition[@href="/content/types/contentTypeId/fieldDefinition/title"]';
124+
125+
return $this->prepareXPathAssertions($xpathAssertions);
126+
}
127+
128+
protected function prepareXPathAssertions(array $xpathAssertions): array
129+
{
130+
return array_map(
131+
static function (string $xpath): array {
132+
return [$xpath];
133+
},
134+
$xpathAssertions
99135
);
100136
}
101137

102-
public function provideXpathAssertions()
138+
protected function getXpathAssertions(): array
103139
{
104-
$xpathAssertions = [
105-
'/FieldDefinition[@href="/content/types/contentTypeId/fieldDefinitions/fieldDefinitionId_23"]',
140+
return [
106141
'/FieldDefinition[@media-type="application/vnd.ibexa.api.FieldDefinition+xml"]',
107142
'/FieldDefinition/id[text()="fieldDefinitionId_23"]',
108143
'/FieldDefinition/identifier[text()="title"]',
@@ -117,33 +152,30 @@ public function provideXpathAssertions()
117152
'/FieldDefinition/names/value[@languageCode="eng-US" and text()="Sindelfingen"]',
118153
'/FieldDefinition/descriptions/value[@languageCode="eng-GB" and text()="Bielefeld"]',
119154
];
120-
121-
return array_map(
122-
static function ($xpath) {
123-
return [$xpath];
124-
},
125-
$xpathAssertions
126-
);
127155
}
128156

129157
/**
130-
* @param string $xpath
131-
* @param \DOMDocument $dom
132-
*
133158
* @depends testVisitRestFieldDefinition
134159
* @dataProvider provideXpathAssertions
135160
*/
136-
public function testGeneratedXml($xpath, \DOMDocument $dom)
161+
public function testGeneratedXml(string $xpath, \DOMDocument $dom): void
162+
{
163+
$this->assertXPath($dom, $xpath);
164+
}
165+
166+
/**
167+
* @depends testVisitRestFieldDefinitionWithPath
168+
* @dataProvider provideXpathAssertionsPath
169+
*/
170+
public function testGeneratedXmlPath(string $xpath, \DOMDocument $dom): void
137171
{
138172
$this->assertXPath($dom, $xpath);
139173
}
140174

141175
/**
142176
* Get the Content visitor.
143-
*
144-
* @return \Ibexa\Rest\Server\Output\ValueObjectVisitor\RestFieldDefinition
145177
*/
146-
protected function internalGetVisitor()
178+
protected function internalGetVisitor(): ValueObjectVisitor\RestFieldDefinition
147179
{
148180
return new ValueObjectVisitor\RestFieldDefinition($this->fieldTypeSerializerMock);
149181
}

0 commit comments

Comments
 (0)