3 namespace Drupal\KernelTests\Core\Theme;
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;
15 * Tests white-listing of entity properties.
19 class TwigWhiteListTest extends KernelTestBase {
22 * Term for referencing.
24 * @var \Drupal\taxonomy\TermInterface
31 * @var \Drupal\Core\Template\TwigEnvironment
38 public static $modules = ['node', 'taxonomy', 'user', 'system', 'text', 'field', 'entity_reference'];
43 protected function setUp() {
45 $this->installSchema('system', ['sequences']);
46 $this->installEntitySchema('node');
47 $this->installEntitySchema('user');
48 $this->installEntitySchema('taxonomy_term');
51 'name' => 'Basic page',
52 'display_submitted' => FALSE,
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,
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,
76 $vocabulary->id() => $vocabulary->id(),
78 'auto_create' => TRUE,
80 // Add the term field.
81 FieldStorageConfig::create([
82 'field_name' => 'field_term',
83 'type' => 'entity_reference',
84 'entity_type' => 'node',
87 'target_type' => 'taxonomy_term',
91 'field_name' => 'field_term',
92 'entity_type' => 'node',
96 'handler' => 'default',
97 'handler_settings' => $handler_settings,
101 // Show on default display and teaser.
102 entity_get_display('node', 'page', 'default')
103 ->setComponent('field_term', [
104 'type' => 'entity_reference_label',
107 // Boot twig environment.
108 $this->twig = \Drupal::service('twig');
112 * Tests white-listing of methods doesn't interfere with chaining.
114 public function testWhiteListChaining() {
115 $node = Node::create([
117 'title' => 'Some node mmk',
119 'field_term' => $this->term->id(),
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');