Upgraded drupal core with security updates
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Theme / TwigWhiteListTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Theme;
4
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\node\Entity\Node;
10 use Drupal\node\Entity\NodeType;
11 use Drupal\taxonomy\Entity\Term;
12 use Drupal\taxonomy\Entity\Vocabulary;
13
14 /**
15  * Tests white-listing of entity properties.
16  *
17  * @group Theme
18  */
19 class TwigWhiteListTest extends KernelTestBase {
20
21   /**
22    * Term for referencing.
23    *
24    * @var \Drupal\taxonomy\TermInterface
25    */
26   protected $term;
27
28   /**
29    * Twig environment.
30    *
31    * @var \Drupal\Core\Template\TwigEnvironment
32    */
33   protected $twig;
34
35   /**
36    * {@inheritdoc}
37    */
38   public static $modules = ['node', 'taxonomy', 'user', 'system', 'text', 'field', 'entity_reference'];
39
40   /**
41    * {@inheritdoc}
42    */
43   protected function setUp() {
44     parent::setUp();
45     $this->installSchema('system', ['sequences']);
46     $this->installEntitySchema('node');
47     $this->installEntitySchema('user');
48     $this->installEntitySchema('taxonomy_term');
49     NodeType::create([
50       'type' => 'page',
51       'name' => 'Basic page',
52       'display_submitted' => FALSE,
53     ])->save();
54     // Add a vocabulary so we can test different view modes.
55     $vocabulary = Vocabulary::create([
56       'name' => $this->randomMachineName(),
57       'description' => $this->randomMachineName(),
58       'vid' => $this->randomMachineName(),
59       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
60       'help' => '',
61     ]);
62     $vocabulary->save();
63
64     // Add a term to the vocabulary.
65     $this->term = Term::create([
66       'name' => 'Sometimes people are just jerks',
67       'description' => $this->randomMachineName(),
68       'vid' => $vocabulary->id(),
69       'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
70     ]);
71     $this->term->save();
72
73     // Create a field.
74     $handler_settings = [
75       'target_bundles' => [
76         $vocabulary->id() => $vocabulary->id(),
77       ],
78       'auto_create' => TRUE,
79     ];
80     // Add the term field.
81     FieldStorageConfig::create([
82       'field_name' => 'field_term',
83       'type' => 'entity_reference',
84       'entity_type' => 'node',
85       'cardinality' => 1,
86       'settings' => [
87         'target_type' => 'taxonomy_term',
88       ],
89     ])->save();
90     FieldConfig::create([
91       'field_name' => 'field_term',
92       'entity_type' => 'node',
93       'bundle' => 'page',
94       'label' => 'Terms',
95       'settings' => [
96         'handler' => 'default',
97         'handler_settings' => $handler_settings,
98       ],
99     ])->save();
100
101     // Show on default display and teaser.
102     entity_get_display('node', 'page', 'default')
103       ->setComponent('field_term', [
104         'type' => 'entity_reference_label',
105       ])
106       ->save();
107     // Boot twig environment.
108     $this->twig = \Drupal::service('twig');
109   }
110
111   /**
112    * Tests white-listing of methods doesn't interfere with chaining.
113    */
114   public function testWhiteListChaining() {
115     $node = Node::create([
116       'type' => 'page',
117       'title' => 'Some node mmk',
118       'status' => 1,
119       'field_term' => $this->term->id(),
120     ]);
121     $node->save();
122     $this->setRawContent(twig_render_template(drupal_get_path('theme', 'test_theme') . '/templates/node.html.twig', ['node' => $node]));
123     $this->assertText('Sometimes people are just jerks');
124   }
125
126 }