Security update for Core, with self-updated composer
[yaffs-website] / web / modules / contrib / entity_reference_revisions / src / Tests / EntityReferenceRevisionsAutocompleteTest.php
1 <?php
2
3 namespace Drupal\entity_reference_revisions\Tests;
4
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\Component\Utility\Html;
7 use Drupal\field_ui\Tests\FieldUiTestTrait;
8 use Drupal\node\Entity\Node;
9 use Drupal\simpletest\WebTestBase;
10
11 /**
12  * Tests the entity_reference_revisions autocomplete.
13  *
14  * @group entity_reference_revisions
15  */
16 class EntityReferenceRevisionsAutocompleteTest extends WebTestBase {
17
18   use FieldUiTestTrait;
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = array(
26     'block_content',
27     'node',
28     'field',
29     'entity_reference_revisions',
30     'field_ui',
31   );
32
33   /**
34    * {@inheritdoc}
35    */
36   protected function setUp() {
37     parent::setUp();
38     // Create article content type.
39     $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
40     // Place the breadcrumb, tested in fieldUIAddNewField().
41     $this->drupalPlaceBlock('system_breadcrumb_block');
42   }
43
44   /**
45    * Test for autocomplete processing.
46    *
47    * Tests that processing does not crash when the entity types of the
48    * referenced entity and of the entity the field is attached to are different.
49    */
50   public function testEntityReferenceRevisionsAutocompleteProcessing() {
51     $admin_user = $this->drupalCreateUser(array(
52       'administer site configuration',
53       'administer nodes',
54       'administer blocks',
55       'create article content',
56       'administer content types',
57       'administer node fields',
58       'administer node display',
59       'administer node form display',
60       'edit any article content',
61     ));
62     $this->drupalLogin($admin_user);
63
64     // Create a custom block content bundle.
65     $this->createBlockContentType(array('type' => 'customblockcontent', 'name' => 'Custom Block Content'));
66
67     // Create entity reference revisions field attached to article.
68     static::fieldUIAddNewField(
69       'admin/structure/types/manage/article',
70       'entity_reference_revisions',
71       'Entity reference revisions',
72       'entity_reference_revisions',
73       array('settings[target_type]' => 'block_content', 'cardinality' => '-1'),
74       array('settings[handler_settings][target_bundles][customblockcontent]' => TRUE)
75     );
76
77     // Create custom block.
78     $block_label = $this->randomMachineName();
79     $block_content = $this->randomString();
80     $edit = array(
81       'info[0][value]' => $block_label,
82       'body[0][value]' => $block_content,
83     );
84     $this->drupalPostForm('block/add', $edit, t('Save'));
85     $block = $this->drupalGetBlockByInfo($block_label);
86
87     // Create an article.
88     $title = $this->randomMachineName();
89     $edit = array(
90       'title[0][value]' => $title,
91       'body[0][value]' => 'Revision 1',
92       'field_entity_reference_revisions[0][target_id]' => $block_label . ' (' . $block->id() . ')',
93     );
94     $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
95     $this->assertText($title);
96     $this->assertText(Html::escape($block_content));
97
98     // Check if the block content is not deleted since there is no composite
99     // relationship.
100     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
101     $node = Node::load($node->id());
102     $node->delete();
103     $this->assertNotNull(BlockContent::load($block->id()));
104   }
105
106   /**
107    * Get a custom block from the database based on its title.
108    *
109    * @param $info
110    *   A block title, usually generated by $this->randomMachineName().
111    * @param $reset
112    *   (optional) Whether to reset the entity cache.
113    *
114    * @return \Drupal\block\BlockInterface
115    *   A block entity matching $info.
116    */
117   function drupalGetBlockByInfo($info, $reset = FALSE) {
118     if ($reset) {
119       \Drupal::entityTypeManager()->getStorage('block_content')->resetCache();
120     }
121     $blocks = \Drupal::entityTypeManager()->getStorage('block_content')->loadByProperties(array('info' => $info));
122     // Get the first block returned from the database.
123     $returned_block = reset($blocks);
124     return $returned_block;
125   }
126
127   /**
128    * Create a block_content bundle.
129    *
130    * @param $parameters
131    *   An assoc array with name (human readable) and type (bundle machine name)
132    *   as keys.
133    */
134   function createBlockContentType($parameters) {
135     $label = $parameters['name'];
136     $machine_name = $parameters['type'];
137     $edit = array(
138       'label' => $label,
139       'id' => $machine_name,
140       'revision' => TRUE,
141     );
142     $this->drupalPostForm('admin/structure/block/block-content/types/add', $edit, t('Save'));
143     $this->assertText($label);
144   }
145
146 }