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 {
19 use EntityReferenceRevisionsCoreVersionUiTestTrait;
26 public static $modules = array(
30 'entity_reference_revisions',
37 protected function setUp() {
39 // Create article content type.
40 $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
41 // Place the breadcrumb, tested in fieldUIAddNewField().
42 $this->drupalPlaceBlock('system_breadcrumb_block');
46 * Test for autocomplete processing.
48 * Tests that processing does not crash when the entity types of the
49 * referenced entity and of the entity the field is attached to are different.
51 public function testEntityReferenceRevisionsAutocompleteProcessing() {
52 $admin_user = $this->drupalCreateUser(array(
53 'administer site configuration',
56 'create article content',
57 'administer content types',
58 'administer node fields',
59 'administer node display',
60 'administer node form display',
61 'edit any article content',
63 $this->drupalLogin($admin_user);
65 // Create a custom block content bundle.
66 $this->createBlockContentType(array('type' => 'customblockcontent', 'name' => 'Custom Block Content'));
68 // Create entity reference revisions field attached to article.
69 static::fieldUIAddNewField(
70 'admin/structure/types/manage/article',
71 'entity_reference_revisions',
72 'Entity reference revisions',
73 'entity_reference_revisions',
74 array('settings[target_type]' => 'block_content', 'cardinality' => '-1'),
75 array('settings[handler_settings][target_bundles][customblockcontent]' => TRUE)
78 // Create custom block.
79 $block_label = $this->randomMachineName();
80 $block_content = $this->randomString();
82 'info[0][value]' => $block_label,
83 'body[0][value]' => $block_content,
85 $this->drupalPostForm('block/add', $edit, t('Save'));
86 $block = $this->drupalGetBlockByInfo($block_label);
89 $title = $this->randomMachineName();
91 'title[0][value]' => $title,
92 'body[0][value]' => 'Revision 1',
93 'field_entity_reference_revisions[0][target_id]' => $block_label . ' (' . $block->id() . ')',
95 $this->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
96 $this->assertText($title);
97 $this->assertText(Html::escape($block_content));
99 // Check if the block content is not deleted since there is no composite
101 $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
102 $node = Node::load($node->id());
104 $this->assertNotNull(BlockContent::load($block->id()));
108 * Get a custom block from the database based on its title.
111 * A block title, usually generated by $this->randomMachineName().
113 * (optional) Whether to reset the entity cache.
115 * @return \Drupal\block\BlockInterface
116 * A block entity matching $info.
118 function drupalGetBlockByInfo($info, $reset = FALSE) {
120 \Drupal::entityTypeManager()->getStorage('block_content')->resetCache();
122 $blocks = \Drupal::entityTypeManager()->getStorage('block_content')->loadByProperties(array('info' => $info));
123 // Get the first block returned from the database.
124 $returned_block = reset($blocks);
125 return $returned_block;
129 * Create a block_content bundle.
132 * An assoc array with name (human readable) and type (bundle machine name)
135 function createBlockContentType($parameters) {
136 $label = $parameters['name'];
137 $machine_name = $parameters['type'];
140 'id' => $machine_name,
143 $this->drupalPostForm('admin/structure/block/block-content/types/add', $edit, t('Save'));
144 $this->assertText($label);