Browse Source

Added tests.

8.x-1.x 8.x-1.4
Chi 8 years ago
parent
commit
d13f2323ae
  1. 2
      src/TwigExtension.php
  2. 124
      tests/src/Functional/TwigTweakTest.php
  3. 16
      tests/twig_tweak_test/config/install/block.block.powered_by_drupal.yml
  4. 193
      tests/twig_tweak_test/config/install/views.view.twig_tweak_test.yml
  5. 15
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig
  6. 8
      tests/twig_tweak_test/twig_tweak_test.info.yml
  7. 20
      tests/twig_tweak_test/twig_tweak_test.module

2
src/TwigExtension.php

@ -237,7 +237,7 @@ class TwigExtension extends \Twig_Extension {
* The new text if matches are found, otherwise unchanged text. * The new text if matches are found, otherwise unchanged text.
*/ */
public function pregReplaceFilter($text, $pattern, $replacement) { public function pregReplaceFilter($text, $pattern, $replacement) {
return preg_replace('/' . preg_quote($pattern, '/') . '/', $replacement, $text); return preg_replace("/$pattern/", $replacement, $text);
} }
/** /**

124
tests/src/Functional/TwigTweakTest.php

@ -0,0 +1,124 @@
<?php
namespace Drupal\Tests\twig_tweak\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Tests twig_tweak twig extension.
*
* @group twig_tweak
*/
class TwigTweakTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'twig_tweak',
'twig_tweak_test',
'views',
'node',
'block',
'image',
];
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
$this->createContentType(['type' => 'page']);
$this->createNode(['title' => 'Alpha']);
$this->createNode(['title' => 'Beta']);
$this->createNode(['title' => 'Gamma']);
}
/**
* Tests output produced by the twig extension.
*/
public function testOutput() {
$this->drupalGet('<front>');
// Test default views display.
$xpath = '//div[@class = "tt-view-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';
$this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and . = "Alpha"]');
$this->assertByXpath($xpath . '//a[contains(@href, "/node/2") and . = "Beta"]');
$this->assertByXpath($xpath . '//a[contains(@href, "/node/3") and . = "Gamma"]');
// Test page_1 view display.
$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[@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/2") and . = "Beta"]');
$this->assertByXpath($xpath . '//a[contains(@href, "/node/3") and . = "Gamma"]');
// Test view argument.
$xpath = '//div[@class = "tt-view-page_1-with-argument"]';
$xpath .= '//div[contains(@class, "view-twig-tweak-test")]';
$xpath .= '/div[@class = "view-content"]//ul[count(./li) = 1]/li';
$this->assertByXpath($xpath . '//a[contains(@href, "/node/1") and . = "Alpha"]');
// Test entity default view mode.
$xpath = '//div[@class = "tt-entity-default"]';
$xpath .= '/article[contains(@class, "node") and not(contains(@class, "node--view-mode-teaser"))]';
$xpath .= '/h2/a/span[. = "Alpha"]';
$this->assertByXpath($xpath);
// Test entity teaser view mode.
$xpath = '//div[@class = "tt-entity-teaser"]';
$xpath .= '/article[contains(@class, "node") and contains(@class, "node--view-mode-teaser")]';
$xpath .= '/h2/a/span[. = "Alpha"]';
$this->assertByXpath($xpath);
// Test loading entity from url.
$xpath = '//div[@class = "tt-entity-from-url" and . = ""]';
$this->assertByXpath($xpath);
$this->drupalGet('/node/2');
$xpath = '//div[@class = "tt-entity-from-url"]';
$xpath .= '/article[contains(@class, "node")]';
$xpath .= '/h2/a/span[. = "Beta"]';
$this->assertByXpath($xpath);
// Test field.
$xpath = '//div[@class = "tt-field"]/div[contains(@class, "field--name-body")]/p[. != ""]';
$this->assertByXpath($xpath);
// Test block.
$xpath = '//div[@class = "tt-block"]';
$xpath .= '/div[@id="block-powered-by-drupal"]/span[contains(., "Powered by Drupal")]';
$this->assertByXpath($xpath);
// Test token.
$xpath = '//div[@class = "tt-token" and . = "Drupal"]';
$this->assertByXpath($xpath);
// Test config.
$xpath = '//div[@class = "tt-config" and . = "Anonymous"]';
$this->assertByXpath($xpath);
// Test token replacement.
$xpath = '//div[@class = "tt-token-replace" and . = "Site name: Drupal"]';
$this->assertByXpath($xpath);
// Test preg replacement.
$xpath = '//div[@class = "tt-preg-replace" and . = "foo-bar"]';
$this->assertByXpath($xpath);
// Test image style.
$xpath = '//div[@class = "tt-image-style" and contains(., "styles/thumbnail/public/images/ocean.jpg")]';
$this->assertByXpath($xpath);
}
/**
* Checks that an element specified by a the xpath exists on the current page.
*/
public function assertByXpath($xpath) {
$this->assertSession()->elementExists('xpath', $xpath);
}
}

16
tests/twig_tweak_test/config/install/block.block.powered_by_drupal.yml

@ -0,0 +1,16 @@
langcode: en
status: true
dependencies:
module:
- system
id: powered_by_drupal
region: main
weight: 10
provider: null
plugin: system_powered_by_block
settings:
id: system_powered_by_block
label: 'Powered by Drupal'
provider: system
label_display: '0'
visibility: { }

193
tests/twig_tweak_test/config/install/views.view.twig_tweak_test.yml

@ -0,0 +1,193 @@
langcode: en
status: true
dependencies:
module:
- node
- user
id: twig_tweak_test
label: 'Twig tweak test'
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
core: 8.x
display:
default:
display_plugin: default
id: default
display_title: Master
position: 0
display_options:
access:
type: perm
options:
perm: 'access content'
cache:
type: tag
options: { }
query:
type: views_query
options:
disable_sql_rewrite: false
distinct: false
replica: false
query_comment: ''
query_tags: { }
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: none
options:
offset: 0
style:
type: html_list
row:
type: fields
options:
default_field_elements: true
inline: { }
separator: ''
hide_empty: false
fields:
title:
id: title
table: node_field_data
field: title
entity_type: node
entity_field: title
label: ''
alter:
alter_text: false
make_link: false
absolute: false
trim: false
word_boundary: false
ellipsis: false
strip_tags: false
html: false
hide_empty: false
empty_zero: false
settings:
link_to_entity: true
plugin_id: field
relationship: none
group_type: group
admin_label: ''
exclude: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_alter_empty: true
click_sort_column: value
type: string
group_column: value
group_columns: { }
group_rows: true
delta_limit: 0
delta_offset: 0
delta_reversed: false
delta_first_last: false
multi_type: separator
separator: ', '
field_api_classes: false
filters: { }
sorts:
nid:
id: nid
table: node_field_data
field: nid
relationship: none
group_type: group
admin_label: ''
order: ASC
exposed: false
expose:
label: ''
entity_type: node
entity_field: nid
plugin_id: standard
title: 'Twig tweak test'
header: { }
footer: { }
empty: { }
relationships: { }
arguments:
nid:
id: nid
table: node_field_data
field: nid
relationship: none
group_type: group
admin_label: ''
default_action: ignore
exception:
value: all
title_enable: false
title: All
title_enable: false
title: ''
default_argument_type: fixed
default_argument_options:
argument: ''
default_argument_skip_url: false
summary_options:
base_path: ''
count: true
items_per_page: 25
override: false
summary:
sort_order: asc
number_of_records: 0
format: default_summary
specify_validation: false
validate:
type: none
fail: 'not found'
validate_options: { }
break_phrase: false
not: false
entity_type: node
entity_field: nid
plugin_id: node_nid
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- 'user.node_grants:view'
- user.permissions
tags: { }
page_1:
display_plugin: page
id: page_1
display_title: Page
position: 1
display_options:
display_extenders: { }
path: twig-tweak-test
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url
- 'user.node_grants:view'
- user.permissions
tags: { }

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

@ -0,0 +1,15 @@
<div class="tt-test" style="background-color: lavender">
<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-with-argument">{{ drupal_view('twig_tweak_test', 'page_1', 1) }}</div>
<div class="tt-entity-default">{{ drupal_entity('node', 1) }}</div>
<div class="tt-entity-teaser">{{ drupal_entity('node', 1, 'teaser') }}</div>
<div class="tt-entity-from-url">{{ drupal_entity('node') }}</div>
<div class="tt-field">{{ drupal_field('body', 'node', 1) }}</div>
<div class="tt-block">{{ drupal_block('powered_by_drupal') }}</div>
<div class="tt-token">{{ drupal_token('site:name') }}</div>
<div class="tt-config">{{ drupal_config('user.settings', 'anonymous') }}</div>
<div class="tt-token-replace">{{ 'Site name: [site:name]' | token_replace }}</div>
<div class="tt-preg-replace">{{ 'foo' | preg_replace('(foo)', '$1-bar') }}</div>
<div class="tt-image-style">{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}</div>
</div>

8
tests/twig_tweak_test/twig_tweak_test.info.yml

@ -0,0 +1,8 @@
name: Twig tweak test
type: module
description: Support module for Tweak twig testing.
package: Testing
core: 8.x
dependencies:
- twig_tweak
- node

20
tests/twig_tweak_test/twig_tweak_test.module

@ -0,0 +1,20 @@
<?php
/**
* @file
* Primary module hooks for Twig Tweak test module.
*/
/**
* Implements hook_page_bottom().
*/
function twig_tweak_test_page_bottom(array &$page_bottom) {
$page_bottom['twig_tweak_test']['#theme'] = 'twig_tweak_test';
}
/**
* Implements hook_theme().
*/
function twig_tweak_test_theme() {
return ['twig_tweak_test' => ['variables' => []]];
}
Loading…
Cancel
Save