Skip to content

Commit

Permalink
Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aschempp committed Jul 10, 2020
1 parent f4ca10f commit 61bccf7
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 81 deletions.
Expand Up @@ -39,7 +39,7 @@ class RegisterPagesPass implements CompilerPassInterface
*/
public function process(ContainerBuilder $container): void
{
if (!$container->has(PageRegistry::class) || !$container->has('contao.routing.candidates')) {
if (!$container->has(PageRegistry::class)) {
return;
}

Expand Down
Expand Up @@ -231,10 +231,10 @@ private function renderArticlePasteAfterButton(DataContainer $dc, array $row, bo
}

if (
('cut' === $clipboard['mode'] && $clipboard['id'] === $row['id'])
$cr
|| ('cut' === $clipboard['mode'] && $clipboard['id'] === $row['id'])
|| ('cutAll' === $clipboard['mode'] && \in_array($row['id'], $clipboard['id'], true))
|| !$this->security->isGranted(ContaoCorePermissions::USER_CAN_EDIT_ARTICLE_HIERARCHY, $row)
|| $cr
|| !$this->security->isGranted(ContaoCorePermissions::USER_CAN_EDIT_ARTICLE_HIERARCHY, $pageModel)
) {
return $this->image->getHtml('pasteafter_.svg').' ';
}
Expand Down
10 changes: 5 additions & 5 deletions core-bundle/src/Routing/Page/RouteConfig.php
Expand Up @@ -32,19 +32,19 @@ final class RouteConfig
/**
* @var array
*/
private $default;
private $defaults;

/**
* @var array
*/
private $methods;

public function __construct(string $pathParameters = null, array $requirements = [], array $options = [], array $default = [], array $methods = [])
public function __construct(string $pathParameters = null, array $requirements = [], array $options = [], array $defaults = [], array $methods = [])
{
$this->pathParameters = $pathParameters;
$this->requirements = $requirements;
$this->options = $options;
$this->default = $default;
$this->defaults = $defaults;
$this->methods = $methods;
}

Expand All @@ -63,9 +63,9 @@ public function getOptions(): array
return $this->options;
}

public function getDefault(): array
public function getDefaults(): array
{
return $this->default;
return $this->defaults;
}

public function getMethods(): array
Expand Down
2 changes: 1 addition & 1 deletion core-bundle/src/Routing/RouteFactory.php
Expand Up @@ -52,7 +52,7 @@ public function createRouteForPage(PageModel $pageModel, string $defaultParamete
{
$config = $this->pageRegistry->getRouteConfig($pageModel->type);
$pathParameters = $config->getPathParameters();
$defaults = $config->getDefault();
$defaults = $config->getDefaults();
$requirements = $config->getRequirements();

if (null === $pathParameters) {
Expand Down
Expand Up @@ -18,6 +18,7 @@
use Contao\CoreBundle\Framework\Adapter;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\Routing\Page\PageRegistry;
use Contao\CoreBundle\Security\ContaoCorePermissions;
use Contao\CoreBundle\Tests\TestCase;
use Contao\DataContainer;
use Contao\DC_Table;
Expand Down Expand Up @@ -552,34 +553,8 @@ public function moduleConfigProvider(): \Generator
];
}

public function testCannotPasteArticleWithoutBackendUser(): void
{
/** @var FrontendUser&MockObject $user */
$user = $this->mockClassWithProperties(FrontendUser::class, ['id' => 1]);

$this->security
->expects($this->atLeastOnce())
->method('getUser')
->willReturn($user)
;

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_article', 'activeRecord' => (object) $this->pageRecord]);

$this->imageAdapter
->expects($this->never())
->method('getHtml')
;

$this->assertSame(
'',
$this->listener->renderArticlePasteButton($dc, $this->pageRecord, 'tl_page', false)
);
}

public function testCannotPasteIntoArticleIfProviderDoesNotSupportContentComposition(): void
{
$this->expectUser();
$page = $this->expectPageWithRow($this->pageRecord);
$this->expectSupportsContentComposition(false, $page);

Expand All @@ -591,6 +566,11 @@ public function testCannotPasteIntoArticleIfProviderDoesNotSupportContentComposi
->method('getHtml')
;

$this->security
->expects($this->never())
->method('isGranted')
;

$this->assertSame(
'',
$this->listener->renderArticlePasteButton($dc, $this->pageRecord, 'tl_page', false)
Expand All @@ -599,7 +579,6 @@ public function testCannotPasteIntoArticleIfProviderDoesNotSupportContentComposi

public function testCannotPasteIntoArticleIfPageLayoutDoesNotHaveArticles(): void
{
$this->expectUser();
$page = $this->expectPageWithRow($this->pageRecord, 1);
$this->expectSupportsContentComposition(true, $page);

Expand All @@ -611,6 +590,11 @@ public function testCannotPasteIntoArticleIfPageLayoutDoesNotHaveArticles(): voi
->method('getHtml')
;

$this->security
->expects($this->never())
->method('isGranted')
;

$this->assertSame(
'',
$this->listener->renderArticlePasteButton($dc, $this->pageRecord, 'tl_page', false)
Expand All @@ -619,7 +603,6 @@ public function testCannotPasteIntoArticleIfPageLayoutDoesNotHaveArticles(): voi

public function testDisablesPasteIntoArticleOnCircularReference(): void
{
$this->expectUser();
$page = $this->expectPageWithRow($this->pageRecord, 0);
$this->expectSupportsContentComposition(true, $page);

Expand All @@ -630,6 +613,11 @@ public function testDisablesPasteIntoArticleOnCircularReference(): void
->willReturn('<img src="pasteinto_.svg">')
;

$this->security
->expects($this->never())
->method('isGranted')
;

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_article', 'activeRecord' => (object) $this->pageRecord]);

Expand All @@ -641,14 +629,13 @@ public function testDisablesPasteIntoArticleOnCircularReference(): void

public function testDisablesPasteIntoArticleIfUserDoesNotHavePermission(): void
{
$user = $this->expectUser();
$page = $this->expectPageWithRow($this->pageRecord, 0);
$this->expectSupportsContentComposition(true, $page);

$user
$this->security
->expects($this->once())
->method('isAllowed')
->with(BackendUser::CAN_EDIT_ARTICLE_HIERARCHY, $this->pageRecord)
->method('isGranted')
->with(ContaoCorePermissions::USER_CAN_EDIT_ARTICLE_HIERARCHY, $this->pageRecord)
->willReturn(false)
;

Expand All @@ -670,14 +657,13 @@ public function testDisablesPasteIntoArticleIfUserDoesNotHavePermission(): void

public function testCanPasteIntoArticle(): void
{
$user = $this->expectUser();
$page = $this->expectPageWithRow($this->pageRecord, 0);
$this->expectSupportsContentComposition(true, $page);

$user
$this->security
->expects($this->once())
->method('isAllowed')
->with(BackendUser::CAN_EDIT_ARTICLE_HIERARCHY, $this->pageRecord)
->method('isGranted')
->with(ContaoCorePermissions::USER_CAN_EDIT_ARTICLE_HIERARCHY, $this->pageRecord)
->willReturn(true)
;

Expand Down Expand Up @@ -705,8 +691,6 @@ public function testCanPasteIntoArticle(): void

public function testCannotPasteAfterArticleIfPageIsNotFound(): void
{
$this->expectUser();

$this->pageModelAdapter
->expects($this->once())
->method('findByPk')
Expand All @@ -718,6 +702,11 @@ public function testCannotPasteAfterArticleIfPageIsNotFound(): void
->method('supportsContentComposition')
;

$this->security
->expects($this->never())
->method('isGranted')
;

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_article', 'activeRecord' => (object) $this->articleRecord]);

Expand All @@ -734,14 +723,17 @@ public function testCannotPasteAfterArticleIfPageIsNotFound(): void

public function testCannotPasteAfterArticleIfProviderDoesNotSupportContentComposition(): void
{
$this->expectUser();

$page = $this->expectPageFindByPk(17, $this->pageRecord);
$this->expectSupportsContentComposition(false, $page);

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_article', 'activeRecord' => (object) $this->articleRecord]);

$this->security
->expects($this->never())
->method('isGranted')
;

$this->imageAdapter
->expects($this->never())
->method('getHtml')
Expand All @@ -755,14 +747,17 @@ public function testCannotPasteAfterArticleIfProviderDoesNotSupportContentCompos

public function testCannotPasteAfterArticleIfPageLayoutDoesNotHaveArticles(): void
{
$this->expectUser();

$page = $this->expectPageFindByPk(17, $this->pageRecord, 17);
$this->expectSupportsContentComposition(true, $page);

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_article', 'activeRecord' => (object) $this->articleRecord]);

$this->security
->expects($this->never())
->method('isGranted')
;

$this->imageAdapter
->expects($this->never())
->method('getHtml')
Expand All @@ -776,14 +771,17 @@ public function testCannotPasteAfterArticleIfPageLayoutDoesNotHaveArticles(): vo

public function testDisablesPasteAfterArticleOnCutCurrentRecord(): void
{
$this->expectUser();

$page = $this->expectPageFindByPk(17, $this->pageRecord, 0);
$this->expectSupportsContentComposition(true, $page);

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_article', 'activeRecord' => (object) $this->articleRecord]);

$this->security
->expects($this->never())
->method('isGranted')
;

$this->imageAdapter
->expects($this->once())
->method('getHtml')
Expand All @@ -799,14 +797,17 @@ public function testDisablesPasteAfterArticleOnCutCurrentRecord(): void

public function testDisablesPasteAfterArticleOnCutAllCurrentRecord(): void
{
$this->expectUser();

$page = $this->expectPageFindByPk(17, $this->pageRecord, 0);
$this->expectSupportsContentComposition(true, $page);

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_article', 'activeRecord' => (object) $this->articleRecord]);

$this->security
->expects($this->never())
->method('isGranted')
;

$this->imageAdapter
->expects($this->once())
->method('getHtml')
Expand All @@ -822,14 +823,17 @@ public function testDisablesPasteAfterArticleOnCutAllCurrentRecord(): void

public function testDisablesPasteAfterArticleOnCircularReference(): void
{
$this->expectUser();

$page = $this->expectPageFindByPk(17, $this->pageRecord, 0);
$this->expectSupportsContentComposition(true, $page);

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_article', 'activeRecord' => (object) $this->articleRecord]);

$this->security
->expects($this->never())
->method('isGranted')
;

$this->imageAdapter
->expects($this->once())
->method('getHtml')
Expand All @@ -839,26 +843,25 @@ public function testDisablesPasteAfterArticleOnCircularReference(): void

$this->assertSame(
'<img src="pasteafter_.svg"> ',
$this->listener->renderArticlePasteButton($dc, $this->articleRecord, 'tl_article', false, ['mode' => 'paste', 'id' => 17])
$this->listener->renderArticlePasteButton($dc, $this->articleRecord, 'tl_article', true, ['mode' => 'paste', 'id' => 17])
);
}

public function testDisablesPasteAfterArticleIfUserDoesNotHavePermission(): void
{
$user = $this->expectUser();
$user
->expects($this->once())
->method('isAllowed')
->with(BackendUser::CAN_EDIT_ARTICLE_HIERARCHY, $this->pageRecord)
->willReturn(false)
;

$page = $this->expectPageFindByPk(17, $this->pageRecord, 0);
$this->expectSupportsContentComposition(true, $page);

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_article', 'activeRecord' => (object) $this->articleRecord]);

$this->security
->expects($this->once())
->method('isGranted')
->with(ContaoCorePermissions::USER_CAN_EDIT_ARTICLE_HIERARCHY, $page)
->willReturn(false)
;

$this->imageAdapter
->expects($this->once())
->method('getHtml')
Expand All @@ -874,20 +877,19 @@ public function testDisablesPasteAfterArticleIfUserDoesNotHavePermission(): void

public function testCanPasteAfterArticle(): void
{
$user = $this->expectUser();
$user
->expects($this->once())
->method('isAllowed')
->with(BackendUser::CAN_EDIT_ARTICLE_HIERARCHY, $this->pageRecord)
->willReturn(true)
;

$page = $this->expectPageFindByPk(17, $this->pageRecord, 0);
$this->expectSupportsContentComposition(true, $page);
$pageModel = $this->expectPageFindByPk(17, $this->pageRecord, 0);
$this->expectSupportsContentComposition(true, $pageModel);

/** @var DataContainer&MockObject $dc */
$dc = $this->mockClassWithProperties(DC_Table::class, ['id' => 17, 'table' => 'tl_article', 'activeRecord' => (object) $this->articleRecord]);

$this->security
->expects($this->once())
->method('isGranted')
->with(ContaoCorePermissions::USER_CAN_EDIT_ARTICLE_HIERARCHY, $pageModel)
->willReturn(true)
;

$this->imageAdapter
->expects($this->once())
->method('getHtml')
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon.dist
Expand Up @@ -48,10 +48,6 @@ parameters:
- %currentWorkingDirectory%/core-bundle/src/DataCollector/ContaoDataCollector.php
- %currentWorkingDirectory%/core-bundle/src/Image/ImageFactory.php

- message: '#Result of \|\| is always false\.#'
paths:
- %currentWorkingDirectory%/core-bundle/src/DependencyInjection/Compiler/RegisterPagesPass.php

- message: '#Strict comparison using === between true and false will always evaluate to false\.#'
path: %currentWorkingDirectory%/core-bundle/src/DataCollector/ContaoDataCollector.php

Expand Down

0 comments on commit 61bccf7

Please sign in to comment.