Skip to content

Commit

Permalink
add test for new extensions & modify SonataAdminExtensionTest
Browse files Browse the repository at this point in the history
  • Loading branch information
tambait committed Dec 10, 2020
1 parent c47aa75 commit f92f8fc
Show file tree
Hide file tree
Showing 4 changed files with 436 additions and 1 deletion.
239 changes: 239 additions & 0 deletions tests/Twig/Extension/CanonicalizeExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Tests\Twig\Extension;

use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Twig\Extension\CanonicalizeExtension;
use Symfony\Bridge\Twig\AppVariable;
use Symfony\Component\HttpFoundation\Request;

/**
* Test for SonataAdminExtension.
*
* @author Andrej Hudec <[email protected]>
*/
final class CanonicalizeExtensionTest extends TestCase
{
/**
* @var SonataAdminExtension
*/
private $twigExtension;

protected function setUp(): void
{
$this->twigExtension = new CanonicalizeExtension();
}

/**
* @dataProvider momentLocalesProvider
*/
public function testCanonicalizedLocaleForMoment(?string $expected, string $original): void
{
$this->assertSame($expected, $this->twigExtension->getCanonicalizedLocaleForMoment($this->mockExtensionContext($original)));
}

/**
* @dataProvider select2LocalesProvider
*/
public function testCanonicalizedLocaleForSelect2(?string $expected, string $original): void
{
$this->assertSame($expected, $this->twigExtension->getCanonicalizedLocaleForSelect2($this->mockExtensionContext($original)));
}

public function momentLocalesProvider(): array
{
return [
['af', 'af'],
['ar-dz', 'ar-dz'],
['ar', 'ar'],
['ar-ly', 'ar-ly'],
['ar-ma', 'ar-ma'],
['ar-sa', 'ar-sa'],
['ar-tn', 'ar-tn'],
['az', 'az'],
['be', 'be'],
['bg', 'bg'],
['bn', 'bn'],
['bo', 'bo'],
['br', 'br'],
['bs', 'bs'],
['ca', 'ca'],
['cs', 'cs'],
['cv', 'cv'],
['cy', 'cy'],
['da', 'da'],
['de-at', 'de-at'],
['de', 'de'],
['de', 'de-de'],
['dv', 'dv'],
['el', 'el'],
[null, 'en'],
[null, 'en-us'],
['en-au', 'en-au'],
['en-ca', 'en-ca'],
['en-gb', 'en-gb'],
['en-ie', 'en-ie'],
['en-nz', 'en-nz'],
['eo', 'eo'],
['es-do', 'es-do'],
['es', 'es-ar'],
['es', 'es-mx'],
['es', 'es'],
['et', 'et'],
['eu', 'eu'],
['fa', 'fa'],
['fi', 'fi'],
['fo', 'fo'],
['fr-ca', 'fr-ca'],
['fr-ch', 'fr-ch'],
['fr', 'fr-fr'],
['fr', 'fr'],
['fy', 'fy'],
['gd', 'gd'],
['gl', 'gl'],
['he', 'he'],
['hi', 'hi'],
['hr', 'hr'],
['hu', 'hu'],
['hy-am', 'hy-am'],
['id', 'id'],
['is', 'is'],
['it', 'it'],
['ja', 'ja'],
['jv', 'jv'],
['ka', 'ka'],
['kk', 'kk'],
['km', 'km'],
['ko', 'ko'],
['ky', 'ky'],
['lb', 'lb'],
['lo', 'lo'],
['lt', 'lt'],
['lv', 'lv'],
['me', 'me'],
['mi', 'mi'],
['mk', 'mk'],
['ml', 'ml'],
['mr', 'mr'],
['ms', 'ms'],
['ms-my', 'ms-my'],
['my', 'my'],
['nb', 'nb'],
['ne', 'ne'],
['nl-be', 'nl-be'],
['nl', 'nl'],
['nl', 'nl-nl'],
['nn', 'nn'],
['pa-in', 'pa-in'],
['pl', 'pl'],
['pt-br', 'pt-br'],
['pt', 'pt'],
['ro', 'ro'],
['ru', 'ru'],
['se', 'se'],
['si', 'si'],
['sk', 'sk'],
['sl', 'sl'],
['sq', 'sq'],
['sr-cyrl', 'sr-cyrl'],
['sr', 'sr'],
['ss', 'ss'],
['sv', 'sv'],
['sw', 'sw'],
['ta', 'ta'],
['te', 'te'],
['tet', 'tet'],
['th', 'th'],
['tlh', 'tlh'],
['tl-ph', 'tl-ph'],
['tr', 'tr'],
['tzl', 'tzl'],
['tzm', 'tzm'],
['tzm-latn', 'tzm-latn'],
['uk', 'uk'],
['uz', 'uz'],
['vi', 'vi'],
['x-pseudo', 'x-pseudo'],
['yo', 'yo'],
['zh-cn', 'zh-cn'],
['zh-hk', 'zh-hk'],
['zh-tw', 'zh-tw'],
];
}

public function select2LocalesProvider()
{
return [
['ar', 'ar'],
['az', 'az'],
['bg', 'bg'],
['ca', 'ca'],
['cs', 'cs'],
['da', 'da'],
['de', 'de'],
['el', 'el'],
[null, 'en'],
['es', 'es'],
['et', 'et'],
['eu', 'eu'],
['fa', 'fa'],
['fi', 'fi'],
['fr', 'fr'],
['gl', 'gl'],
['he', 'he'],
['hr', 'hr'],
['hu', 'hu'],
['id', 'id'],
['is', 'is'],
['it', 'it'],
['ja', 'ja'],
['ka', 'ka'],
['ko', 'ko'],
['lt', 'lt'],
['lv', 'lv'],
['mk', 'mk'],
['ms', 'ms'],
['nb', 'nb'],
['nl', 'nl'],
['pl', 'pl'],
['pt-PT', 'pt'],
['pt-BR', 'pt-BR'],
['pt-PT', 'pt-PT'],
['ro', 'ro'],
['rs', 'rs'],
['ru', 'ru'],
['sk', 'sk'],
['sv', 'sv'],
['th', 'th'],
['tr', 'tr'],
['ug-CN', 'ug'],
['ug-CN', 'ug-CN'],
['uk', 'uk'],
['vi', 'vi'],
['zh-CN', 'zh'],
['zh-CN', 'zh-CN'],
['zh-TW', 'zh-TW'],
];
}

private function mockExtensionContext(string $locale): array
{
$request = $this->createMock(Request::class);
$request->method('getLocale')->willReturn($locale);
$appVariable = $this->createMock(AppVariable::class);
$appVariable->method('getRequest')->willReturn($request);

return ['app' => $appVariable];
}
}
49 changes: 49 additions & 0 deletions tests/Twig/Extension/SecurityExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\AdminBundle\Tests\Twig\Extension;

use PHPUnit\Framework\TestCase;
use Sonata\AdminBundle\Twig\Extension\SecurityExtension;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

/**
* Test for SonataAdminExtension.
*
* @author Andrej Hudec <[email protected]>
*/
final class SecurityExtensionTest extends TestCase
{
public function testIsGrantedAffirmative(): void
{
$securityChecker = $this->createStub(AuthorizationCheckerInterface::class);
$twigExtension = new SecurityExtension($securityChecker);

$securityChecker
->method('isGranted')
->withConsecutive(
['foo', null],
['bar', null],
['foo', null],
['bar', null]
)
->willReturnMap([
['foo', null, false],
['bar', null, true],
]);

$this->assertTrue($twigExtension->isGrantedAffirmative(['foo', 'bar']));
$this->assertFalse($twigExtension->isGrantedAffirmative('foo'));
$this->assertTrue($twigExtension->isGrantedAffirmative('bar'));
}
}
34 changes: 33 additions & 1 deletion tests/Twig/Extension/SonataAdminExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ protected function setUp(): void
$propertyAccessor,
$this->securityChecker
);
$this->twigExtension->setXEditableTypeMapping($this->xEditableTypeMapping);

$this->twigExtension->setXEditableTypeMapping($this->xEditableTypeMapping, 'sonata_deprecation_mute');

$request = $this->createMock(Request::class);
$request->method('get')->with('_sonata_admin')->willReturn('sonata_admin_foo_service');
Expand Down Expand Up @@ -2753,6 +2754,9 @@ public function testGetUrlsafeIdentifier_GivenAdmin_Bar(): void
$this->assertSame(1234567, $this->twigExtension->getUrlSafeIdentifier($model, $this->adminBar));
}

/**
* NEXT_MAJOR: Remove this method.
*/
public function xEditableChoicesProvider()
{
return [
Expand Down Expand Up @@ -2799,7 +2803,11 @@ public function xEditableChoicesProvider()
}

/**
* NEXT_MAJOR: Remove this method.
*
* @dataProvider xEditablechoicesProvider
*
* @group legacy
*/
public function testGetXEditableChoicesIsIdempotent(array $options, array $expectedChoices): void
{
Expand All @@ -2818,6 +2826,8 @@ public function testGetXEditableChoicesIsIdempotent(array $options, array $expec
$options['multiple'] ?? null
));

$this->expectDeprecation('The Sonata\AdminBundle\Twig\Extension\SonataAdminExtension::getXEditableChoices method is deprecated in favor of XEditableExtension::getXEditableChoices since version 3.x and will be removed in 4.0.');

$this->assertSame($expectedChoices, $this->twigExtension->getXEditableChoices($fieldDescription));
}

Expand Down Expand Up @@ -2877,13 +2887,22 @@ public function select2LocalesProvider()
}

/**
* NEXT_MAJOR: Remove this method.
*
* @dataProvider select2LocalesProvider
*
* @group legacy
*/
public function testCanonicalizedLocaleForSelect2(?string $expected, string $original): void
{
$this->expectDeprecation('The Sonata\AdminBundle\Twig\Extension\SonataAdminExtension::getCanonicalizedLocaleForSelect2 method is deprecated in favor of XEditableExtension::getCanonicalizedLocaleForSelect2 since version 3.x and will be removed in 4.0.');

$this->assertSame($expected, $this->twigExtension->getCanonicalizedLocaleForSelect2($this->mockExtensionContext($original)));
}

/**
* NEXT_MAJOR: Remove this method.
*/
public function momentLocalesProvider(): array
{
return [
Expand Down Expand Up @@ -3006,15 +3025,28 @@ public function momentLocalesProvider(): array
}

/**
* NEXT_MAJOR: Remove this method.
*
* @dataProvider momentLocalesProvider
*
* @group legacy
*/
public function testCanonicalizedLocaleForMoment(?string $expected, string $original): void
{
$this->expectDeprecation('The Sonata\AdminBundle\Twig\Extension\SonataAdminExtension::getCanonicalizedLocaleForMoment method is deprecated in favor of XEditableExtension::getCanonicalizedLocaleForMoment since version 3.x and will be removed in 4.0.');

$this->assertSame($expected, $this->twigExtension->getCanonicalizedLocaleForMoment($this->mockExtensionContext($original)));
}

/**
* NEXT_MAJOR: Remove this method.
*
* @group legacy
*/
public function testIsGrantedAffirmative(): void
{
$this->expectDeprecation('The Sonata\AdminBundle\Twig\Extension\SonataAdminExtension::isGrantedAffirmative method is deprecated in favor of SecurityExtension::isGrantedAffirmative since version 3.x and will be removed in 4.0.');

$this->securityChecker
->method('isGranted')
->withConsecutive(
Expand Down
Loading

0 comments on commit f92f8fc

Please sign in to comment.