Version 1
[yaffs-website] / web / core / modules / system / src / Tests / Theme / TwigFilterTest.php
1 <?php
2
3 namespace Drupal\system\Tests\Theme;
4
5 use Drupal\simpletest\WebTestBase;
6
7 /**
8  * Tests Drupal's Twig filters.
9  *
10  * @group Theme
11  */
12 class TwigFilterTest extends WebTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var array
18    */
19   public static $modules = ['twig_theme_test'];
20
21   /**
22    * Test Twig "without" filter.
23    */
24   public function testTwigWithoutFilter() {
25     $filter_test = [
26       '#theme' => 'twig_theme_test_filter',
27       '#quote' => [
28         'content' => ['#markup' => 'You can only find truth with logic if you have already found truth without it.'],
29         'author' => ['#markup' => 'Gilbert Keith Chesterton'],
30         'date' => ['#markup' => '1874-1936'],
31       ],
32       '#attributes' => [
33         'id' => 'quotes',
34         'checked' => TRUE,
35         'class' => ['red', 'green', 'blue'],
36       ],
37     ];
38     $rendered = \Drupal::service('renderer')->renderRoot($filter_test);
39     $this->setRawContent($rendered);
40
41     $elements = [
42       [
43         'expected' => '<div><strong>No author:</strong> You can only find truth with logic if you have already found truth without it.1874-1936.</div>',
44         'message' => '"No author" was successfully rendered.',
45       ],
46       [
47         'expected' => '<div><strong>Complete quote after without:</strong> You can only find truth with logic if you have already found truth without it.Gilbert Keith Chesterton1874-1936.</div>',
48         'message' => '"Complete quote after without" was successfully rendered.',
49       ],
50       [
51         'expected' => '<div><strong>Only author:</strong> Gilbert Keith Chesterton.</div>',
52         'message' => '"Only author:" was successfully rendered.',
53       ],
54       [
55         'expected' => '<div><strong>No author or date:</strong> You can only find truth with logic if you have already found truth without it..</div>',
56         'message' => '"No author or date" was successfully rendered.',
57       ],
58       [
59         'expected' => '<div><strong>Only date:</strong> 1874-1936.</div>',
60         'message' => '"Only date" was successfully rendered.',
61       ],
62       [
63         'expected' => '<div><strong>Complete quote again for good measure:</strong> You can only find truth with logic if you have already found truth without it.Gilbert Keith Chesterton1874-1936.</div>',
64         'message' => '"Complete quote again for good measure" was successfully rendered.',
65       ],
66       [
67         'expected' => '<div><strong>Marked-up:</strong>
68   <blockquote>
69     <p>You can only find truth with logic if you have already found truth without it.</p>
70     <footer>
71       &ndash; <cite><a href="#">Gilbert Keith Chesterton</a> <em>(1874-1936)</em></cite>
72     </footer>
73   </blockquote>',
74         'message' => '"Marked-up quote" was successfully rendered.',
75       ],
76       [
77         'expected' => '<div><span id="quotes" checked class="red green blue">All attributes:</span></div>',
78         'message' => 'All attributes printed.',
79       ],
80       [
81         'expected' => '<div><span class="red green blue" id="quotes" checked>Class attributes in front, remainder at the back:</span></div>',
82         'message' => 'Class attributes printed in the front, the rest in the back.',
83       ],
84       [
85         'expected' => '<div><span id="quotes" checked data-class="red green blue">Class attributes in back, remainder at the front:</span></div>',
86         'message' => 'Class attributes printed in the back, the rest in the front.',
87       ],
88       [
89         'expected' => '<div><span class="red green blue">Class attributes only:</span></div>',
90         'message' => 'Class attributes only printed.',
91       ],
92       [
93         'expected' => '<div><span checked id="quotes" class="red green blue">Without boolean attribute.</span></div>',
94         'message' => 'Boolean attribute printed in the front.',
95       ],
96       [
97         'expected' => '<div><span data-id="quotes" checked class="red green blue">Without string attribute.</span></div>',
98         'message' => 'Without string attribute in the front.',
99       ],
100       [
101         'expected' => '<div><span checked>Without id and class attributes.</span></div>',
102         'message' => 'Attributes printed without id and class attributes.',
103       ],
104       [
105         'expected' => '<div><span id="quotes" checked class="red green blue">All attributes again.</span></div>',
106         'message' => 'All attributes printed again.',
107       ],
108       [
109         'expected' => '<div id="quotes-here"><span class="gray-like-a-bunny bem__ized--top-feature" id="quotes-here">ID and class. Having the same ID twice is not valid markup but we want to make sure the filter doesn\'t use \Drupal\Component\Utility\Html::getUniqueId().</span></div>',
110         'message' => 'Class and ID filtered.',
111       ],
112       [
113         'expected' => '<div><strong>Rendered author string length:</strong> 24.</div>',
114         'message' => 'Render filter string\'s length.',
115       ],
116     ];
117
118     foreach ($elements as $element) {
119       $this->assertRaw($element['expected'], $element['message']);
120     }
121   }
122
123 }