Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn committed Aug 7, 2024
1 parent a1b04af commit b6328ce
Show file tree
Hide file tree
Showing 3 changed files with 247 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,12 @@ private function createQueryBuilder(array $filters, array $sortBy = [], array $s
$queryBuilder->setFirstResult($offset);
}

if (\array_key_exists('locale', $filters) // should also work with locale = null
&& \array_key_exists('stage', $filters)) {
if ((
\array_key_exists('locale', $filters) // should also work with locale = null
&& \array_key_exists('stage', $filters)
)
|| ([] === $filters && [] !== $sortBy) // if no filters are set, but sortBy is set
) {
$this->dimensionContentQueryEnhancer->addFilters(
$queryBuilder,
'example',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace Sulu\Bundle\ContentBundle\Tests\Functional\Content\Infrastructure\Doctrine;

use Sulu\Bundle\ContactBundle\Entity\Contact;
use Sulu\Bundle\ContentBundle\Content\Application\ContentManager\ContentManagerInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollection;
use Sulu\Bundle\ContentBundle\Content\Infrastructure\Doctrine\DimensionContentQueryEnhancer;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent;
Expand All @@ -35,9 +37,12 @@ class DimensionContentQueryEnhancerTest extends SuluTestCase
*/
private $exampleRepository;

private ContentManagerInterface $contentManager;

protected function setUp(): void
{
$this->exampleRepository = static::getContainer()->get('example_test.example_repository');
$this->contentManager = static::getContainer()->get('sulu_content.content_manager');
}

public function testNullDimensionAttribute(): void
Expand Down Expand Up @@ -395,4 +400,231 @@ public function testFilterTemplateKeys(): void
'templateKeys' => ['a', 'c'],
]));
}

public function testSortByTitle(): void
{
static::purgeDatabase();

$example = static::createExample();
$example2 = static::createExample();
$example3 = static::createExample();
static::createExampleContent($example, ['templateData' => ['title' => 'Example A'], 'templateKey' => 'a']);
static::createExampleContent($example2, ['templateData' => ['title' => 'Example B'], 'templateKey' => 'b']);
static::createExampleContent($example3, ['templateData' => ['title' => 'Example C'], 'templateKey' => 'c']);
static::getEntityManager()->flush();
static::getEntityManager()->clear();

$result = \iterator_to_array(
$this->exampleRepository->findBy(
[
'locale' => 'en',
'stage' => 'draft',
],
[
'title' => 'asc',
]
)
);
$this->assertCount(3, $result);
$this->assertSame('Example A', $this->contentManager->resolve($result[0], ['locale' => 'en', 'stage' => 'draft'])->getTemplateData()['title']);
$this->assertSame('Example B', $this->contentManager->resolve($result[1], ['locale' => 'en', 'stage' => 'draft'])->getTemplateData()['title']);
$this->assertSame('Example C', $this->contentManager->resolve($result[2], ['locale' => 'en', 'stage' => 'draft'])->getTemplateData()['title']);

$result = \iterator_to_array(
$this->exampleRepository->findBy(
[
'locale' => 'en',
'stage' => 'draft',
],
[
'title' => 'desc',
]
)
);
$this->assertCount(3, $result);
$this->assertSame('Example C', $this->contentManager->resolve($result[0], ['locale' => 'en', 'stage' => 'draft'])->getTemplateData()['title']);
$this->assertSame('Example B', $this->contentManager->resolve($result[1], ['locale' => 'en', 'stage' => 'draft'])->getTemplateData()['title']);
$this->assertSame('Example A', $this->contentManager->resolve($result[2], ['locale' => 'en', 'stage' => 'draft'])->getTemplateData()['title']);
}

public function testSortByAuthored(): void
{
static::purgeDatabase();

$example = static::createExample();
$example2 = static::createExample();
$example3 = static::createExample();
static::createExampleContent($example, ['templateData' => ['title' => 'Example A'], 'authored' => new \DateTimeImmutable('2020-01-01')]);
static::createExampleContent($example2, ['templateData' => ['title' => 'Example B'], 'authored' => new \DateTimeImmutable('2020-03-01')]);
static::createExampleContent($example3, ['templateData' => ['title' => 'Example C'], 'authored' => new \DateTimeImmutable('2020-02-01')]);
static::getEntityManager()->flush();
static::getEntityManager()->clear();

$result = \iterator_to_array(
$this->exampleRepository->findBy(
[
'locale' => 'en',
'stage' => 'draft',
],
[
'authored' => 'desc',
]
)
);
$this->assertCount(3, $result);
/** @var ExampleDimensionContent $exampleDimensionContent */
$exampleDimensionContent = $this->contentManager->resolve($result[0], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-03-01', $exampleDimensionContent->getAuthored()?->format('Y-m-d'));
/** @var ExampleDimensionContent $exampleDimensionContent2 */
$exampleDimensionContent2 = $this->contentManager->resolve($result[1], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-02-01', $exampleDimensionContent2->getAuthored()?->format('Y-m-d'));
/** @var ExampleDimensionContent $exampleDimensionContent3 */
$exampleDimensionContent3 = $this->contentManager->resolve($result[2], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-01-01', $exampleDimensionContent3->getAuthored()?->format('Y-m-d'));

$result = \iterator_to_array(
$this->exampleRepository->findBy(
[
'locale' => 'en',
'stage' => 'draft',
],
[
'authored' => 'asc',
]
)
);
$this->assertCount(3, $result);
/** @var ExampleDimensionContent $exampleDimensionContent */
$exampleDimensionContent = $this->contentManager->resolve($result[0], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-01-01', $exampleDimensionContent->getAuthored()?->format('Y-m-d'));
/** @var ExampleDimensionContent $exampleDimensionContent2 */
$exampleDimensionContent2 = $this->contentManager->resolve($result[1], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-02-01', $exampleDimensionContent2->getAuthored()?->format('Y-m-d'));
/** @var ExampleDimensionContent $exampleDimensionContent3 */
$exampleDimensionContent3 = $this->contentManager->resolve($result[2], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-03-01', $exampleDimensionContent3->getAuthored()?->format('Y-m-d'));
}

public function testSortByWorkflowPublished(): void
{
static::purgeDatabase();

$example = static::createExample();
$example2 = static::createExample();
$example3 = static::createExample();
static::createExampleContent($example, ['templateData' => ['title' => 'Example A'], 'workflowPublished' => new \DateTimeImmutable('2020-01-01')]);
static::createExampleContent($example2, ['templateData' => ['title' => 'Example B'], 'workflowPublished' => new \DateTimeImmutable('2020-03-01')]);
static::createExampleContent($example3, ['templateData' => ['title' => 'Example C'], 'workflowPublished' => new \DateTimeImmutable('2020-02-01')]);
static::getEntityManager()->flush();
static::getEntityManager()->clear();

$result = \iterator_to_array(
$this->exampleRepository->findBy(
[
'locale' => 'en',
'stage' => 'draft',
],
[
'workflowPublished' => 'desc',
]
)
);
$this->assertCount(3, $result);
/** @var ExampleDimensionContent $exampleDimensionContent */
$exampleDimensionContent = $this->contentManager->resolve($result[0], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-03-01', $exampleDimensionContent->getWorkflowPublished()?->format('Y-m-d'));
/** @var ExampleDimensionContent $exampleDimensionContent2 */
$exampleDimensionContent2 = $this->contentManager->resolve($result[1], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-02-01', $exampleDimensionContent2->getWorkflowPublished()?->format('Y-m-d'));
/** @var ExampleDimensionContent $exampleDimensionContent3 */
$exampleDimensionContent3 = $this->contentManager->resolve($result[2], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-01-01', $exampleDimensionContent3->getWorkflowPublished()?->format('Y-m-d'));

$result = \iterator_to_array(
$this->exampleRepository->findBy(
[
'locale' => 'en',
'stage' => 'draft',
],
[
'workflowPublished' => 'asc',
]
)
);
$this->assertCount(3, $result);
/** @var ExampleDimensionContent $exampleDimensionContent */
$exampleDimensionContent = $this->contentManager->resolve($result[0], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-01-01', $exampleDimensionContent->getWorkflowPublished()?->format('Y-m-d'));
/** @var ExampleDimensionContent $exampleDimensionContent2 */
$exampleDimensionContent2 = $this->contentManager->resolve($result[1], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-02-01', $exampleDimensionContent2->getWorkflowPublished()?->format('Y-m-d'));
/** @var ExampleDimensionContent $exampleDimensionContent3 */
$exampleDimensionContent3 = $this->contentManager->resolve($result[2], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame('2020-03-01', $exampleDimensionContent3->getWorkflowPublished()?->format('Y-m-d'));
}

public function testSortByAuthor(): void
{
static::purgeDatabase();

$contact = (new Contact())->setFirstName('A')->setLastName('Doe')->setFormOfAddress(0);
$contact2 = (new Contact())->setFirstName('B')->setLastName('Doe')->setFormOfAddress(0);
$contact3 = (new Contact())->setFirstName('C')->setLastName('Doe')->setFormOfAddress(0);
static::getEntityManager()->persist($contact);
static::getEntityManager()->persist($contact2);
static::getEntityManager()->persist($contact3);

$example = static::createExample();
$example2 = static::createExample();
$example3 = static::createExample();
static::createExampleContent($example, ['templateData' => ['title' => 'Example A'], 'author' => $contact]);
static::createExampleContent($example2, ['templateData' => ['title' => 'Example B'], 'author' => $contact2]);
static::createExampleContent($example3, ['templateData' => ['title' => 'Example C'], 'author' => $contact3]);
static::getEntityManager()->flush();
static::getEntityManager()->clear();

$result = \iterator_to_array(
$this->exampleRepository->findBy(
[
'locale' => 'en',
'stage' => 'draft',
],
[
'author' => 'asc',
]
)
);
$this->assertCount(3, $result);
/** @var ExampleDimensionContent $exampleDimensionContent */
$exampleDimensionContent = $this->contentManager->resolve($result[0], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame($contact->getId(), $exampleDimensionContent->getAuthor()?->getId());
/** @var ExampleDimensionContent $exampleDimensionContent2 */
$exampleDimensionContent2 = $this->contentManager->resolve($result[1], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame($contact2->getId(), $exampleDimensionContent2->getAuthor()?->getId());
/** @var ExampleDimensionContent $exampleDimensionContent3 */
$exampleDimensionContent3 = $this->contentManager->resolve($result[2], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame($contact3->getId(), $exampleDimensionContent3->getAuthor()?->getId());

$result = \iterator_to_array(
$this->exampleRepository->findBy(
[
'locale' => 'en',
'stage' => 'draft',
],
[
'author' => 'desc',
]
)
);
$this->assertCount(3, $result);
/** @var ExampleDimensionContent $exampleDimensionContent */
$exampleDimensionContent = $this->contentManager->resolve($result[2], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame($contact->getId(), $exampleDimensionContent->getAuthor()?->getId());
/** @var ExampleDimensionContent $exampleDimensionContent2 */
$exampleDimensionContent2 = $this->contentManager->resolve($result[1], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame($contact2->getId(), $exampleDimensionContent2->getAuthor()?->getId());
/** @var ExampleDimensionContent $exampleDimensionContent3 */
$exampleDimensionContent3 = $this->contentManager->resolve($result[0], ['locale' => 'en', 'stage' => 'draft']);
$this->assertSame($contact3->getId(), $exampleDimensionContent3->getAuthor()?->getId());
}
}
9 changes: 9 additions & 0 deletions Tests/Functional/Traits/CreateExampleTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Doctrine\ORM\EntityManagerInterface;
use Sulu\Bundle\CategoryBundle\Entity\CategoryInterface;
use Sulu\Bundle\ContactBundle\Entity\ContactInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent;
Expand All @@ -40,6 +41,10 @@ public function createExample(): Example
* templateData?: mixed[],
* excerptCategories?: CategoryInterface[],
* excerptTags?: TagInterface[],
* author?: ?ContactInterface,
* authored?: ?\DateTimeImmutable,
* workflowPlace?: ?string,
* workflowPublished?: ?\DateTimeImmutable,
* } $data
*/
public function createExampleContent(Example $example, array $data = []): void
Expand All @@ -56,6 +61,10 @@ public function createExampleContent(Example $example, array $data = []): void
$localizedDimensionContent = $example->createDimensionContent();
$localizedDimensionContent->setLocale($locale);
$localizedDimensionContent->setStage($stage);
$localizedDimensionContent->setAuthor($data['author'] ?? null);
$localizedDimensionContent->setAuthored($data['authored'] ?? null);
$localizedDimensionContent->setWorkflowPlace($data['workflowPlace'] ?? null);
$localizedDimensionContent->setWorkflowPublished($data['workflowPublished'] ?? null);

$templateKey = $data['templateKey'] ?? null;
if ($templateKey) {
Expand Down

0 comments on commit b6328ce

Please sign in to comment.