Browse Source

Code cleanup.

8.x-1.x
Chi 8 years ago
parent
commit
542319714d
  1. 9
      README.txt
  2. 7
      src/TwigExtension.php
  3. 44
      tests/src/Functional/TwigTweakTest.php
  4. 19
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig
  5. 0
      tests/twig_tweak_test/twig_tweak_test.links.menu.yml
  6. 3
      twig_tweak.info.yml

9
README.txt

@ -1,6 +1,9 @@
-- SUMMARY -- -- SUMMARY --
Twig Tweak module provides developers a Twig extension with some addition Twig Tweak module provides a Twig extension with some useful functions and
functions and filters. filters. See src/TwigExtension.php for details.
See src/TwigExtension.php for details. -- LINKS --
Project page: https://www.drupal.org/project/twig_tweak
Twig home page: http://twig.sensiolabs.org
Drupal 8 Twig documentation: https://www.drupal.org/docs/8/theming/twig

7
src/TwigExtension.php

@ -145,7 +145,7 @@ class TwigExtension extends \Twig_Extension {
* The region to build. * The region to build.
* @param string $theme * @param string $theme
* (Optional) The name of the theme to load the region. If it is not * (Optional) The name of the theme to load the region. If it is not
* provided default site theme will be used. * provided then default theme will be used.
* *
* @return array * @return array
* A render array to display the region content. * A render array to display the region content.
@ -395,7 +395,10 @@ class TwigExtension extends \Twig_Extension {
* (optional) The message's type. Defaults to 'status'. * (optional) The message's type. Defaults to 'status'.
* @param bool $repeat * @param bool $repeat
* (optional) If this is FALSE and the message is already set, then the * (optional) If this is FALSE and the message is already set, then the
* message won't be repeated. Defaults to FALSE. * message will not be repeated. Defaults to FALSE.
*
* @return array
* A render array to disable caching.
* *
* @see drupal_set_message() * @see drupal_set_message()
*/ */

44
tests/src/Functional/TwigTweakTest.php

@ -44,64 +44,64 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-view-default"]'; $xpath = '//div[@class = "tt-view-default"]';
$xpath .= '//div[contains(@class, "view-twig-tweak-test") and contains(@class, "view-display-id-default")]'; $xpath .= '//div[contains(@class, "view-twig-tweak-test") and contains(@class, "view-display-id-default")]';
$xpath .= '/div[@class = "view-content"]//ul[count(./li) = 3]/li'; $xpath .= '/div[@class = "view-content"]//ul[count(./li) = 3]/li';
$this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and . = "Alpha"]'); $this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and text() = "Alpha"]');
$this->assertByXpath($xpath . '//a[contains(@href, "/node/2") and . = "Beta"]'); $this->assertByXpath($xpath . '//a[contains(@href, "/node/2") and text() = "Beta"]');
$this->assertByXpath($xpath . '//a[contains(@href, "/node/3") and . = "Gamma"]'); $this->assertByXpath($xpath . '//a[contains(@href, "/node/3") and text() = "Gamma"]');
// Test page_1 view display. // Test page_1 view display.
$xpath = '//div[@class = "tt-view-page_1"]'; $xpath = '//div[@class = "tt-view-page_1"]';
$xpath .= '//div[contains(@class, "view-twig-tweak-test") and contains(@class, "view-display-id-page_1")]'; $xpath .= '//div[contains(@class, "view-twig-tweak-test") and contains(@class, "view-display-id-page_1")]';
$xpath .= '/div[@class = "view-content"]//ul[count(./li) = 3]/li'; $xpath .= '/div[@class = "view-content"]//ul[count(./li) = 3]/li';
$this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and . = "Alpha"]'); $this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and text() = "Alpha"]');
$this->assertByXpath($xpath . '//a[contains(@href, "/node/2") and . = "Beta"]'); $this->assertByXpath($xpath . '//a[contains(@href, "/node/2") and text() = "Beta"]');
$this->assertByXpath($xpath . '//a[contains(@href, "/node/3") and . = "Gamma"]'); $this->assertByXpath($xpath . '//a[contains(@href, "/node/3") and text() = "Gamma"]');
// Test view argument. // Test view argument.
$xpath = '//div[@class = "tt-view-page_1-with-argument"]'; $xpath = '//div[@class = "tt-view-page_1-with-argument"]';
$xpath .= '//div[contains(@class, "view-twig-tweak-test")]'; $xpath .= '//div[contains(@class, "view-twig-tweak-test")]';
$xpath .= '/div[@class = "view-content"]//ul[count(./li) = 1]/li'; $xpath .= '/div[@class = "view-content"]//ul[count(./li) = 1]/li';
$this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and . = "Alpha"]'); $this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and text() = "Alpha"]');
// Test entity default view mode. // Test entity default view mode.
$xpath = '//div[@class = "tt-entity-default"]'; $xpath = '//div[@class = "tt-entity-default"]';
$xpath .= '/article[contains(@class, "node") and not(contains(@class, "node--view-mode-teaser"))]'; $xpath .= '/article[contains(@class, "node") and not(contains(@class, "node--view-mode-teaser"))]';
$xpath .= '/h2/a/span[. = "Alpha"]'; $xpath .= '/h2/a/span[text() = "Alpha"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test entity teaser view mode. // Test entity teaser view mode.
$xpath = '//div[@class = "tt-entity-teaser"]'; $xpath = '//div[@class = "tt-entity-teaser"]';
$xpath .= '/article[contains(@class, "node") and contains(@class, "node--view-mode-teaser")]'; $xpath .= '/article[contains(@class, "node") and contains(@class, "node--view-mode-teaser")]';
$xpath .= '/h2/a/span[. = "Alpha"]'; $xpath .= '/h2/a/span[text() = "Alpha"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test loading entity from url. // Test loading entity from url.
$xpath = '//div[@class = "tt-entity-from-url" and . = ""]'; $xpath = '//div[@class = "tt-entity-from-url" and not(text())]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
$this->drupalGet('/node/2'); $this->drupalGet('/node/2');
$xpath = '//div[@class = "tt-entity-from-url"]'; $xpath = '//div[@class = "tt-entity-from-url"]';
$xpath .= '/article[contains(@class, "node")]'; $xpath .= '/article[contains(@class, "node")]';
$xpath .= '/h2/a/span[. = "Beta"]'; $xpath .= '/h2/a/span[text() = "Beta"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test field. // Test field.
$xpath = '//div[@class = "tt-field"]/div[contains(@class, "field--name-body")]/p[. != ""]'; $xpath = '//div[@class = "tt-field"]/div[contains(@class, "field--name-body")]/p[text() != ""]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test menu (default). // Test menu (default).
$xpath = '//div[@class = "tt-menu-default"]/ul[@class = "menu"]/li/a[. = "Link 1"]/../ul[@class = "menu"]/li/ul[@class = "menu"]/li/a[. = "Link 3"]'; $xpath = '//div[@class = "tt-menu-default"]/ul[@class = "menu"]/li/a[text() = "Link 1"]/../ul[@class = "menu"]/li/ul[@class = "menu"]/li/a[text() = "Link 3"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test menu (level). // Test menu (level).
$xpath = '//div[@class = "tt-menu-level"]/ul[@class = "menu"]/li/a[. = "Link 2"]/../ul[@class = "menu"]/li/a[. = "Link 3"]'; $xpath = '//div[@class = "tt-menu-level"]/ul[@class = "menu"]/li/a[text() = "Link 2"]/../ul[@class = "menu"]/li/a[text() = "Link 3"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test menu (depth). // Test menu (depth).
$xpath = '//div[@class = "tt-menu-depth"]/ul[@class = "menu"]/li[not(ul)]/a[. = "Link 1"]'; $xpath = '//div[@class = "tt-menu-depth"]/ul[@class = "menu"]/li[not(ul)]/a[text() = "Link 1"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test region. // Test region.
$xpath = '//div[@class = "tt-region"]'; $xpath = '//div[@class = "tt-region"]';
$xpath .= '/div[contains(@class, "block-page-title-block") and h1[@class="page-title" and . = "Beta"]]'; $xpath .= '/div[contains(@class, "block-page-title-block") and h1[@class="page-title" and text() = "Beta"]]';
$xpath .= '/following-sibling::div[@class="messages messages--warning" and contains(., "Hi!")]'; $xpath .= '/following-sibling::div[@class="messages messages--warning" and contains(., "Hi!")]';
$xpath .= '/following-sibling::div[contains(@class, "block-system-powered-by-block")]/span[. = "Powered by Drupal"]'; $xpath .= '/following-sibling::div[contains(@class, "block-system-powered-by-block")]/span[. = "Powered by Drupal"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
@ -112,15 +112,15 @@ class TwigTweakTest extends BrowserTestBase {
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test token. // Test token.
$xpath = '//div[@class = "tt-token" and . = "Drupal"]'; $xpath = '//div[@class = "tt-token" and text() = "Drupal"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test token with context. // Test token with context.
$xpath = '//div[@class = "tt-token-data" and . = "Beta"]'; $xpath = '//div[@class = "tt-token-data" and text() = "Beta"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test config. // Test config.
$xpath = '//div[@class = "tt-config" and . = "Anonymous"]'; $xpath = '//div[@class = "tt-config" and text() = "Anonymous"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test status message. // Test status message.
@ -128,15 +128,15 @@ class TwigTweakTest extends BrowserTestBase {
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test token replacement. // Test token replacement.
$xpath = '//div[@class = "tt-token-replace" and . = "Site name: Drupal"]'; $xpath = '//div[@class = "tt-token-replace" and text() = "Site name: Drupal"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test preg replacement. // Test preg replacement.
$xpath = '//div[@class = "tt-preg-replace" and . = "foo-bar"]'; $xpath = '//div[@class = "tt-preg-replace" and text() = "foo-bar"]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
// Test image style. // Test image style.
$xpath = '//div[@class = "tt-image-style" and contains(., "styles/thumbnail/public/images/ocean.jpg")]'; $xpath = '//div[@class = "tt-image-style" and contains(text(), "styles/thumbnail/public/images/ocean.jpg")]';
$this->assertByXpath($xpath); $this->assertByXpath($xpath);
} }

19
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -1,4 +1,21 @@
<div class="tt-test" style="background-color: lavender"> <style>
.tt-test > div {
margin: 15px;
padding: 10px;
outline: solid 2px dodgerblue;
}
.tt-test > div::before {
content: attr(class);
font-weight: bold;
display: block;
font-style: italic;
border-bottom: solid 1px dodgerblue;
margin-bottom: 10px;
padding-bottom: 5px;
color: dodgerblue;
}
</style>
<div class="tt-test">
<div class="tt-view-default">{{ drupal_view('twig_tweak_test') }}</div> <div class="tt-view-default">{{ drupal_view('twig_tweak_test') }}</div>
<div class="tt-view-page_1">{{ drupal_view('twig_tweak_test', 'page_1') }}</div> <div class="tt-view-page_1">{{ drupal_view('twig_tweak_test', 'page_1') }}</div>
<div class="tt-view-page_1-with-argument">{{ drupal_view('twig_tweak_test', 'page_1', 1) }}</div> <div class="tt-view-page_1-with-argument">{{ drupal_view('twig_tweak_test', 'page_1', 1) }}</div>

0
twig_tweak.links.menu.yml → tests/twig_tweak_test/twig_tweak_test.links.menu.yml

3
twig_tweak.info.yml

@ -1,5 +1,4 @@
name: Twig tweak name: Twig tweak
type: module type: module
description: Provides some extra twig functions. description: Provides some extra Twig functions and filters.
package: custom
core: 8.x core: 8.x

Loading…
Cancel
Save