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

Fix availableLocales after unpublish #216

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,23 @@ public function onUnpublish(TransitionEvent $transitionEvent): void
}

$liveDimensionAttributes = \array_merge($dimensionAttributes, ['stage' => DimensionContentInterface::STAGE_LIVE]);

$dimensionContentCollection = $this->dimensionContentRepository->load($contentRichEntity, $liveDimensionAttributes);
$localizedLiveDimensionContent = $dimensionContentCollection->getDimensionContent($liveDimensionAttributes);

if (!$localizedLiveDimensionContent) {
throw new ContentNotFoundException($contentRichEntity, $liveDimensionAttributes);
}

$locale = $localizedLiveDimensionContent->getLocale();

if ($locale) {
$unlocalizedLiveDimensionAttributes = \array_merge($liveDimensionAttributes, ['locale' => null]);

/** @var DimensionContentInterface $unlocalizedLiveDimensionContent */
$unlocalizedLiveDimensionContent = $dimensionContentCollection->getDimensionContent($unlocalizedLiveDimensionAttributes);
$unlocalizedLiveDimensionContent->removeAvailableLocale($locale);
}

$this->entityManager->remove($localizedLiveDimensionContent);
}

Expand Down
5 changes: 5 additions & 0 deletions Content/Domain/Model/DimensionContentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public function getGhostLocale(): ?string;
*/
public function addAvailableLocale(string $availableLocale): void;

/**
* @internal should only be set by content bundle services not from outside
*/
public function removeAvailableLocale(string $availableLocale): void;

/**
* @return string[]|null
*/
Expand Down
16 changes: 16 additions & 0 deletions Content/Domain/Model/DimensionContentTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ public function addAvailableLocale(string $availableLocale): void
}
}

/**
* @internal should only be set by content bundle services not from outside
*/
public function removeAvailableLocale(string $availableLocale): void
{
if (null === $this->availableLocales) {
return;
}

$removeIndex = \array_search($availableLocale, $this->availableLocales, true);
if (false !== $removeIndex) {
unset($this->availableLocales[$removeIndex]);
$this->availableLocales = \array_values($this->availableLocales);
}
}

public function getAvailableLocales(): ?array
{
return $this->availableLocales;
Expand Down
4 changes: 2 additions & 2 deletions Tests/Functional/Integration/responses/example_get.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"ghostLocale": "en",
"id": "@integer@",
"images": null,
"mainWebspace": "sulu-io",
"locale": "en",
"published": null,
"publishedState": false,
Expand All @@ -31,6 +32,5 @@
"template": "example-2",
"title": "Test Example",
"url": "/my-example",
"workflowPlace": "unpublished",
"mainWebspace": "sulu-io"
"workflowPlace": "unpublished"
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"ghostLocale": "en",
"id": "@integer@",
"locale": null,
"mainWebspace": null,
"published": null,
"publishedState": false,
"seoCanonicalUrl": null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"id": "@integer@",
"images": null,
"locale": "de",
"mainWebspace": "sulu-io",
"published": null,
"publishedState": false,
"seoCanonicalUrl": "https://sulu.io/",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\WorkflowInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Repository\DimensionContentRepositoryInterface;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\Example;
use Sulu\Bundle\ContentBundle\Tests\Application\ExampleTestBundle\Entity\ExampleDimensionContent;
use Symfony\Component\Workflow\Event\TransitionEvent;
use Symfony\Component\Workflow\Marking;

Expand Down Expand Up @@ -197,12 +199,23 @@ public function testOnUnpublish(): void
$entityManager->reveal()
);

$localizedLiveDimensionContent = $this->prophesize(DimensionContentInterface::class);
$example = new Example();
$localizedLiveDimensionContent = new ExampleDimensionContent($example);
$localizedLiveDimensionContent->setStage('live');
$localizedLiveDimensionContent->setLocale('en');
$dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class);
$dimensionContentCollection->getDimensionContent(['locale' => 'en', 'stage' => 'live'])
->willReturn($localizedLiveDimensionContent)
->shouldBeCalled();

$unlocalizedLiveDimensionContent = new ExampleDimensionContent($example);
$unlocalizedLiveDimensionContent->setStage('live');
$unlocalizedLiveDimensionContent->addAvailableLocale('en');
$unlocalizedLiveDimensionContent->addAvailableLocale('de');
$dimensionContentCollection->getDimensionContent(['locale' => null, 'stage' => 'live'])
->willReturn($unlocalizedLiveDimensionContent)
->shouldBeCalled();

$liveDimensionAttributes = \array_merge($dimensionAttributes, ['stage' => DimensionContentInterface::STAGE_LIVE]);

$dimensionContentRepository->load($contentRichEntity->reveal(), $liveDimensionAttributes)
Expand All @@ -212,5 +225,6 @@ public function testOnUnpublish(): void
$entityManager->remove($localizedLiveDimensionContent)->shouldBeCalled();

$contentUnpublishSubscriber->onUnpublish($event);
$this->assertSame(['de'], $unlocalizedLiveDimensionContent->getAvailableLocales());
}
}
40 changes: 40 additions & 0 deletions Tests/Unit/Content/Domain/Model/DimensionContentTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,46 @@ public function testAddSameAvailableLocale(): void
$this->assertSame(['de'], $model->getAvailableLocales());
}

public function testRemoveAvailableLocaleFirst(): void
{
$model = $this->getDimensionContentInstance();
$this->assertNull($model->getAvailableLocales());
$model->addAvailableLocale('de');
$model->addAvailableLocale('en');
$this->assertSame(['de', 'en'], $model->getAvailableLocales());
$model->removeAvailableLocale('de');
$this->assertSame(['en'], $model->getAvailableLocales());
}

public function testRemoveAvailableLocaleLast(): void
{
$model = $this->getDimensionContentInstance();
$this->assertNull($model->getAvailableLocales());
$model->addAvailableLocale('de');
$model->addAvailableLocale('en');
$this->assertSame(['de', 'en'], $model->getAvailableLocales());
$model->removeAvailableLocale('en');
$this->assertSame(['de'], $model->getAvailableLocales());
}

public function testRemoveAvailableLocaleNotSet(): void
{
$model = $this->getDimensionContentInstance();
$this->assertNull($model->getAvailableLocales());
$model->addAvailableLocale('en');
$this->assertSame(['en'], $model->getAvailableLocales());
$model->removeAvailableLocale('de');
$this->assertSame(['en'], $model->getAvailableLocales());
}

public function testRemoveAvailableLocaleEmpty(): void
{
$model = $this->getDimensionContentInstance();
$this->assertNull($model->getAvailableLocales());
$model->removeAvailableLocale('de');
$this->assertNull($model->getAvailableLocales());
}

public function testGetSetStage(): void
{
$model = $this->getDimensionContentInstance();
Expand Down
5 changes: 5 additions & 0 deletions Tests/Unit/Mocks/DimensionContentMockWrapperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public function getAvailableLocales(): ?array
return $this->instance->getAvailableLocales();
}

public function removeAvailableLocale(string $availableLocale): void
{
$this->instance->removeAvailableLocale();
}

public function addAvailableLocale(string $availableLocale): void
{
$this->instance->addAvailableLocale($availableLocale);
Expand Down
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,10 @@
"doctrine/persistence": "1.3.2"
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"composer/package-versions-deprecated": true
}
},
"autoload": {
"psr-4": {
Expand Down
22 changes: 22 additions & 0 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
parameters:
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update phpstan in another issue as this seems to work in phpstan 1.0

ignoreErrors:
-
message: "#^Parameter \\#1 \\$array of function array_values expects array, array\\<string\\>\\|null given\\.$#"
count: 1
path: Tests/Application/ExampleTestBundle/Entity/ExampleDimensionContent.php

-
message: "#^Parameter \\#1 \\$array of function array_values expects array, array\\<string\\>\\|null given\\.$#"
count: 2
path: Tests/Unit/Content/Application/ContentNormalizer/ContentNormalizerTest.php

-
message: "#^Parameter \\#1 \\$array of function array_values expects array, array\\<string\\>\\|null given\\.$#"
count: 1
path: Tests/Unit/Content/Domain/Model/DimensionContentTraitTest.php

-
message: "#^Parameter \\#1 \\$array of function array_values expects array, array\\<string\\>\\|null given\\.$#"
count: 3
path: Tests/Unit/Content/Infrastructure/Sulu/Admin/ContentViewBuilderFactoryTest.php

1 change: 1 addition & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
includes:
- phpstan-baseline.neon
- vendor/jangregor/phpstan-prophecy/extension.neon
- vendor/phpstan/phpstan-symfony/extension.neon
- vendor/phpstan/phpstan-doctrine/extension.neon
Expand Down