Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Tests] Fixed FieldTest, RestContentTest, and RestUser(Group)Test #86

Merged
merged 9 commits into from
Apr 17, 2024
415 changes: 0 additions & 415 deletions phpstan-baseline.neon

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions phpunit.functional.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
bootstrap="vendor/autoload.php"
beStrictAboutTestsThatDoNotTestAnything="false"
colors="true"
failOnWarning="true"
>
<php>
<env name="EZP_TEST_REST_HOST" value="localhost"/>
Expand Down
1 change: 1 addition & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
bootstrap="vendor/autoload.php"
beStrictAboutTestsThatDoNotTestAnything="false"
colors="true"
failOnWarning="true"
>
<testsuites>
<testsuite name="Ibexa REST Bundle">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Rest\Server\Output\ValueObjectVisitor;

use DateTime;
use DOMDocument;
use Ibexa\Contracts\Core\Repository\Values\Content\ContentInfo;
use Ibexa\Contracts\Core\Repository\Values\Content\Location;
use Ibexa\Core\Repository\Values\ContentType\ContentType;
use Ibexa\Tests\Rest\Output\ValueObjectVisitorBaseTest;

abstract class BaseContentValueObjectVisitorTestCase extends ValueObjectVisitorBaseTest
{
abstract public function testVisitWithoutEmbeddedVersion(): DOMDocument;

abstract protected function getXPathFirstElementName(): string;

protected function getContentInfoStub(): ContentInfo
{
return new ContentInfo(
[
'id' => 22,
'name' => 'Sindelfingen',
'sectionId' => 23,
'currentVersionNo' => 5,
'published' => true,
'ownerId' => 24,
'modificationDate' => new DateTime('2012-09-05 15:27 Europe/Berlin'),
'publishedDate' => new DateTime('2012-09-05 15:27 Europe/Berlin'),
'alwaysAvailable' => true,
'status' => ContentInfo::STATUS_PUBLISHED,
'remoteId' => 'abc123',
'mainLanguageCode' => 'eng-US',
'mainLocationId' => 25,
'contentTypeId' => 26,
'contentType' => new ContentType(['id' => 26]),
'isHidden' => true,
]
);
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testNameCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/name[text()="Sindelfingen"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testVersionsHrefCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/Versions[@href="/content/objects/22/versions"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testVersionsMediaTypeCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/Versions[@media-type="application/vnd.ibexa.api.VersionList+xml"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testSectionHrefCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/Section[@href="/content/sections/23"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testSectionMediaTypeCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/Section[@media-type="application/vnd.ibexa.api.Section+xml"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testMainLocationHrefCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/MainLocation[@href="/content/locations/1/2/23"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testMainLocationMediaTypeCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/MainLocation[@media-type="application/vnd.ibexa.api.Location+xml"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testLocationsHrefCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/Locations[@href="/content/objects/22/locations"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testLocationsMediaTypeCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/Locations[@media-type="application/vnd.ibexa.api.LocationList+xml"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testOwnerHrefCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/Owner[@href="/user/users/24"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testOwnerMediaTypeCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/Owner[@media-type="application/vnd.ibexa.api.User+xml"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testLastModificationDateCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/lastModificationDate[text()="2012-09-05T15:27:00+02:00"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testMainLanguageCodeCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/mainLanguageCode[text()="eng-US"]', $this->getXPathFirstElementName()));
}

/**
* @depends testVisitWithoutEmbeddedVersion
*/
public function testAlwaysAvailableCorrect(DOMDocument $dom): void
{
$this->assertXPath($dom, sprintf('/%s/alwaysAvailable[text()="true"]', $this->getXPathFirstElementName()));
}

protected function addContentRouteExpectations(ContentInfo $contentInfo, Location $location): void
{
$contentId = $contentInfo->getId();
$contentTypeId = $contentInfo->getContentType()->id;
$sectionId = $contentInfo->getSectionId();

$this->addRouteExpectation(
'ibexa.rest.load_content_type',
['contentTypeId' => $contentTypeId],
"/content/types/$contentTypeId"
);
$this->addRouteExpectation(
'ibexa.rest.load_content_versions',
['contentId' => $contentId],
"/content/objects/$contentId/versions"
);
$this->addRouteExpectation(
'ibexa.rest.load_section',
['sectionId' => $sectionId],
"/content/sections/$sectionId"
);
$locationPath = trim($location->getPathString(), '/');
$this->addRouteExpectation(
'ibexa.rest.load_location',
['locationPath' => $locationPath],
"/content/locations/$locationPath"
);
$this->addRouteExpectation(
'ibexa.rest.load_locations_for_content',
['contentId' => $contentId],
"/content/objects/$contentId/locations"
);
}
}
54 changes: 30 additions & 24 deletions tests/lib/Server/Output/ValueObjectVisitor/FieldTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

namespace Ibexa\Tests\Rest\Server\Output\ValueObjectVisitor;

use Ibexa\Contracts\Core\Repository\FieldType;
use Ibexa\Contracts\Core\Repository\FieldTypeService;
use Ibexa\Contracts\Core\Repository\Values\Content\Field as ApiField;
use Ibexa\Contracts\Rest\Output\Generator;
use Ibexa\Rest\FieldTypeProcessorRegistry;
use Ibexa\Rest\Output\FieldTypeSerializer;
use Ibexa\Rest\Server\Output\ValueObjectVisitor\Field;
use Ibexa\Tests\Rest\Output\ValueObjectVisitorBaseTest;
Expand All @@ -19,14 +21,18 @@
*/
final class FieldTest extends ValueObjectVisitorBaseTest
{
/** @var \Ibexa\Rest\Output\FieldTypeSerializer&\PHPUnit\Framework\MockObject\MockObject */
private FieldTypeSerializer $fieldTypeSerializer;
/** @var \Ibexa\Contracts\Core\Repository\FieldTypeService&\PHPUnit\Framework\MockObject\MockObject */
private FieldTypeService $fieldTypeService;

/** @var \Ibexa\Rest\FieldTypeProcessorRegistry&\PHPUnit\Framework\MockObject\MockObject */
private FieldTypeProcessorRegistry $fieldTypeProcessorRegistry;

protected function setUp(): void
{
parent::setUp();

$this->fieldTypeSerializer = $this->createMock(FieldTypeSerializer::class);
$this->fieldTypeService = $this->createMock(FieldTypeService::class);
$this->fieldTypeProcessorRegistry = $this->createMock(FieldTypeProcessorRegistry::class);
}

public function testVisit(): void
Expand All @@ -41,15 +47,11 @@ public function testVisit(): void
'fieldDefIdentifier' => 'foo',
'value' => 'foo',
'languageCode' => 'eng-GB',
'fieldTypeIdentifier' => 'ezfoo',
'fieldTypeIdentifier' => 'foo_field_type',
]
);

$this->mockFieldTypeSerializerSerializeContentFieldValue(
$generator,
$field,
'<value>foo</value>'
);
$this->mockSerializingFieldValue($field);

$visitor->visit(
$this->getVisitorMock(),
Expand All @@ -67,25 +69,24 @@ public function testVisit(): void
$this->assertContainsTag('fieldTypeIdentifier', $result);
}

private function mockFieldTypeSerializerSerializeContentFieldValue(
Generator $generator,
ApiField $field,
string $value
): void {
$this->fieldTypeSerializer
->method('serializeContentFieldValue')
->with(
$generator,
$field
)
->willReturn($value);
private function mockSerializingFieldValue(ApiField $field): void
{
$fieldTypeMock = $this->createMock(FieldType::class);
$fieldTypeIdentifier = $field->getFieldTypeIdentifier();

$fieldTypeMock->method('getFieldTypeIdentifier')->willReturn($fieldTypeIdentifier);
$fieldTypeMock->method('toHash')->with('foo')->willReturn(['value' => 'foo']);

$this->fieldTypeProcessorRegistry->method('hasProcessor')->with($fieldTypeIdentifier)->willReturn(false);

$this->fieldTypeService->method('getFieldType')->with($fieldTypeIdentifier)->willReturn($fieldTypeMock);
}

private function assertContainsTag(
string $tag,
string $result
): void {
$this->assertXMLTag(
self::assertXMLTag(
[
'tag' => $tag,
],
Expand All @@ -96,6 +97,11 @@ private function assertContainsTag(

protected function internalGetVisitor(): Field
{
return new Field($this->fieldTypeSerializer);
return new Field(
new FieldTypeSerializer(
$this->fieldTypeService,
$this->fieldTypeProcessorRegistry
)
);
}
}
Loading
Loading