diff --git a/src/TwigExtension.php b/src/TwigExtension.php index 9bd311d..14fed21 100644 --- a/src/TwigExtension.php +++ b/src/TwigExtension.php @@ -237,7 +237,7 @@ class TwigExtension extends \Twig_Extension { * The new text if matches are found, otherwise unchanged text. */ public function pregReplaceFilter($text, $pattern, $replacement) { - return preg_replace('/' . preg_quote($pattern, '/') . '/', $replacement, $text); + return preg_replace("/$pattern/", $replacement, $text); } /** diff --git a/tests/src/Functional/TwigTweakTest.php b/tests/src/Functional/TwigTweakTest.php new file mode 100644 index 0000000..b47af1a --- /dev/null +++ b/tests/src/Functional/TwigTweakTest.php @@ -0,0 +1,124 @@ +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(''); + + // 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); + } + +} diff --git a/tests/twig_tweak_test/config/install/block.block.powered_by_drupal.yml b/tests/twig_tweak_test/config/install/block.block.powered_by_drupal.yml new file mode 100644 index 0000000..59a2f51 --- /dev/null +++ b/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: { } diff --git a/tests/twig_tweak_test/config/install/views.view.twig_tweak_test.yml b/tests/twig_tweak_test/config/install/views.view.twig_tweak_test.yml new file mode 100644 index 0000000..959fb25 --- /dev/null +++ b/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: { } diff --git a/tests/twig_tweak_test/templates/twig-tweak-test.html.twig b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig new file mode 100644 index 0000000..2cbaa19 --- /dev/null +++ b/tests/twig_tweak_test/templates/twig-tweak-test.html.twig @@ -0,0 +1,15 @@ +
+
{{ drupal_view('twig_tweak_test') }}
+
{{ drupal_view('twig_tweak_test', 'page_1') }}
+
{{ drupal_view('twig_tweak_test', 'page_1', 1) }}
+
{{ drupal_entity('node', 1) }}
+
{{ drupal_entity('node', 1, 'teaser') }}
+
{{ drupal_entity('node') }}
+
{{ drupal_field('body', 'node', 1) }}
+
{{ drupal_block('powered_by_drupal') }}
+
{{ drupal_token('site:name') }}
+
{{ drupal_config('user.settings', 'anonymous') }}
+
{{ 'Site name: [site:name]' | token_replace }}
+
{{ 'foo' | preg_replace('(foo)', '$1-bar') }}
+
{{ 'public://images/ocean.jpg' | image_style('thumbnail') }}
+
diff --git a/tests/twig_tweak_test/twig_tweak_test.info.yml b/tests/twig_tweak_test/twig_tweak_test.info.yml new file mode 100644 index 0000000..5982a93 --- /dev/null +++ b/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 diff --git a/tests/twig_tweak_test/twig_tweak_test.module b/tests/twig_tweak_test/twig_tweak_test.module new file mode 100644 index 0000000..cabd649 --- /dev/null +++ b/tests/twig_tweak_test/twig_tweak_test.module @@ -0,0 +1,20 @@ + ['variables' => []]]; +}