-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
149 additions
and
1 deletion.
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
...s/uebertool-companion/modules/uebertool_twig/tests/src/Kernel/TestTwigExtrasExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Tests\uebertool_twig\Kernel; | ||
|
||
use Drupal\Core\Template\Attribute; | ||
use Drupal\KernelTests\KernelTestBase; | ||
use Drupal\purl\Url; | ||
use Drupal\uebertool_twig\Twig\Extension\TwigExtrasExtension; | ||
use PHPUnit\Framework\Attributes\Group; | ||
|
||
/** | ||
* Test description. | ||
*/ | ||
#[Group('uebertool_twig')] | ||
final class TestTwigExtrasExtension extends KernelTestBase { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $modules = ['uebertool_twig']; | ||
|
||
protected TwigExtrasExtension $twigExtension; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected function setUp(): void { | ||
parent::setUp(); | ||
|
||
$this->twigExtension = $this->container->get('uebertool_twig.twig.extension'); | ||
} | ||
|
||
/** | ||
* Tests linkText. | ||
*/ | ||
public function testLinkText(): void { | ||
$this->assertEmpty($this->twigExtension->linkText([])); | ||
$this->assertEmpty($this->twigExtension->linkText([ | ||
'#type' => 'markup', | ||
])); | ||
$this->assertEmpty($this->twigExtension->linkText([ | ||
[ | ||
'#type' => 'markup', | ||
], | ||
])); | ||
$this->assertSame('Link text', $this->twigExtension->linkText([ | ||
'#type' => 'link', | ||
'#title' => 'Link text', | ||
'#url' => Url::fromRoute('<front>'), | ||
])); | ||
$this->assertSame('Link text', $this->twigExtension->linkText([ | ||
[ | ||
'#type' => 'link', | ||
'#title' => 'Link text', | ||
'#url' => Url::fromRoute('<front>'), | ||
], | ||
])); | ||
} | ||
|
||
/** | ||
* Tests linkUrl. | ||
*/ | ||
public function testLinkUrl(): void { | ||
$this->assertEmpty($this->twigExtension->linkUrl([])); | ||
$this->assertEmpty($this->twigExtension->linkUrl([ | ||
'#type' => 'markup', | ||
])); | ||
$this->assertEmpty($this->twigExtension->linkUrl([ | ||
[ | ||
'#type' => 'markup', | ||
], | ||
])); | ||
$this->assertSame('https://example.com', $this->twigExtension->linkUrl([ | ||
'#type' => 'link', | ||
'#title' => 'Link text', | ||
'#url' => Url::fromUri('https://example.com'), | ||
])); | ||
$this->assertSame('https://example.com', $this->twigExtension->linkUrl([ | ||
[ | ||
'#type' => 'link', | ||
'#title' => 'Link text', | ||
'#url' => Url::fromUri('https://example.com'), | ||
], | ||
])); | ||
} | ||
|
||
/** | ||
* Tests linkUrl. | ||
*/ | ||
public function testLinkAttributes(): void { | ||
$this->assertEquals(new Attribute(), $this->twigExtension->linkAttributes([])); | ||
$this->assertEquals(new Attribute(), $this->twigExtension->linkAttributes([ | ||
'#type' => 'markup', | ||
])); | ||
$this->assertEquals(new Attribute([ | ||
'class' => [ | ||
'foo', | ||
], | ||
]), $this->twigExtension->linkAttributes([ | ||
'#type' => 'link', | ||
'#title' => 'Link text', | ||
'#url' => Url::fromUri('https://example.com'), | ||
'#attributes' => [ | ||
'class' => 'foo', | ||
], | ||
])); | ||
|
||
// Collect #attributes from render array. | ||
$url = Url::fromUri('https://example.com'); | ||
$this->assertEquals(new Attribute([ | ||
'class' => [ | ||
'foo', | ||
], | ||
]), $this->twigExtension->linkAttributes([ | ||
'#type' => 'link', | ||
'#title' => 'Link text', | ||
'#url' => $url, | ||
'#attributes' => [ | ||
'class' => 'foo', | ||
], | ||
])); | ||
|
||
// Collect attributes from link options. | ||
$url = Url::fromUri('https://example.com'); | ||
$url->setOption('attributes', [ | ||
'class' => 'foo', | ||
'data-foo' => 'bar', | ||
]); | ||
$this->assertEquals(new Attribute([ | ||
'class' => [ | ||
'foo', | ||
], | ||
'data-foo' => 'bar', | ||
'target' => '_blank', | ||
]), $this->twigExtension->linkAttributes([ | ||
'#type' => 'link', | ||
'#title' => 'Link text', | ||
'#url' => $url, | ||
'#attributes' => [ | ||
'target' => '_blank', | ||
], | ||
])); | ||
} | ||
|
||
} |
3 changes: 2 additions & 1 deletion
3
packages/uebertool-companion/modules/uebertool_twig/uebertool_twig.services.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
services: | ||
uebertool_twig.twig.extension: | ||
class: Drupal\uebertool_twig\Twig\Extension\TwigExtrasExtension | ||
arguments: ['@renderer'] | ||
tags: | ||
- {name: twig.extension} | ||
- { name: twig.extension } |