3 namespace Drupal\entity_reference_revisions\Tests;
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;
12 * Tests the entity_reference_revisions autocomplete.
14 * @group entity_reference_revisions
16 class EntityReferenceRevisionsAutocompleteTest extends WebTestBase {
25 public static $modules = array(
29 'entity_reference_revisions',
36 protected function 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');
45 * Test for autocomplete processing.
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.
50 public function testEntityReferenceRevisionsAutocompleteProcessing() {
51 $admin_user = $this->drupalCreateUser(array(
52 'administer site configuration',
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',
62 $this->drupalLogin($admin_user);
64 // Create a custom block content bundle.
65 $this->createBlockContentType(array('type' => 'customblockcontent', 'name' => 'Custom Block Content'));
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)
77 // Create custom block.
78 $block_label = $this->randomMachineName();
79 $block_content = $this->randomString();
81 'info[0][value]' => $block_label,
82 'body[0][value]' => $block_content,
84 $this->drupalPostForm('block/add', $edit, t('Save'));
85 $block = $this->drupalGetBlockByInfo($block_label);
88 $title = $this->randomMachineName();
90 'title[0][value]' => $title,
91 'body[0][value]' => 'Revision 1',
92 'field_entity_reference_revisions[0][target_id]' => $block_label . ' (' . $block->id() . ')',
94 $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
95 $this->assertText($title);
96 $this->assertText(Html::escape($block_content));
98 // Check if the block content is not deleted since there is no composite
100 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
101 $node = Node::load($node->id());
103 $this->assertNotNull(BlockContent::load($block->id()));
107 * Get a custom block from the database based on its title.
110 * A block title, usually generated by $this->randomMachineName().
112 * (optional) Whether to reset the entity cache.
114 * @return \Drupal\block\BlockInterface
115 * A block entity matching $info.
117 function drupalGetBlockByInfo($info, $reset = FALSE) {
119 \Drupal::entityTypeManager()->getStorage('block_content')->resetCache();
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;
128 * Create a block_content bundle.
131 * An assoc array with name (human readable) and type (bundle machine name)
134 function createBlockContentType($parameters) {
135 $label = $parameters['name'];
136 $machine_name = $parameters['type'];
139 'id' => $machine_name,
142 $this->drupalPostForm('admin/structure/block/block-content/types/add', $edit, t('Save'));
143 $this->assertText($label);