Upgraded imagemagick and manually altered pdf to image module to handle changes....
[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   use EntityReferenceRevisionsCoreVersionUiTestTrait;
20
21   /**
22    * Modules to enable.
23    *
24    * @var array
25    */
26   public static $modules = array(
27     'block_content',
28     'node',
29     'field',
30     'entity_reference_revisions',
31     'field_ui',
32   );
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::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');
43   }
44
45   /**
46    * Test for autocomplete processing.
47    *
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.
50    */
51   public function testEntityReferenceRevisionsAutocompleteProcessing() {
52     $admin_user = $this->drupalCreateUser(array(
53       'administer site configuration',
54       'administer nodes',
55       'administer blocks',
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',
62     ));
63     $this->drupalLogin($admin_user);
64
65     // Create a custom block content bundle.
66     $this->createBlockContentType(array('type' => 'customblockcontent', 'name' => 'Custom Block Content'));
67
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)
76     );
77
78     // Create custom block.
79     $block_label = $this->randomMachineName();
80     $block_content = $this->randomString();
81     $edit = array(
82       'info[0][value]' => $block_label,
83       'body[0][value]' => $block_content,
84     );
85     $this->drupalPostForm('block/add', $edit, t('Save'));
86     $block = $this->drupalGetBlockByInfo($block_label);
87
88     // Create an article.
89     $title = $this->randomMachineName();
90     $edit = array(
91       'title[0][value]' => $title,
92       'body[0][value]' => 'Revision 1',
93       'field_entity_reference_revisions[0][target_id]' => $block_label . ' (' . $block->id() . ')',
94     );
95     $this->drupalPostNodeForm('node/add/article', $edit, t('Save and publish'));
96     $this->assertText($title);
97     $this->assertText(Html::escape($block_content));
98
99     // Check if the block content is not deleted since there is no composite
100     // relationship.
101     $node = $this->drupalGetNodeByTitle($edit['title[0][value]']);
102     $node = Node::load($node->id());
103     $node->delete();
104     $this->assertNotNull(BlockContent::load($block->id()));
105   }
106
107   /**
108    * Get a custom block from the database based on its title.
109    *
110    * @param $info
111    *   A block title, usually generated by $this->randomMachineName().
112    * @param $reset
113    *   (optional) Whether to reset the entity cache.
114    *
115    * @return \Drupal\block\BlockInterface
116    *   A block entity matching $info.
117    */
118   function drupalGetBlockByInfo($info, $reset = FALSE) {
119     if ($reset) {
120       \Drupal::entityTypeManager()->getStorage('block_content')->resetCache();
121     }
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;
126   }
127
128   /**
129    * Create a block_content bundle.
130    *
131    * @param $parameters
132    *   An assoc array with name (human readable) and type (bundle machine name)
133    *   as keys.
134    */
135   function createBlockContentType($parameters) {
136     $label = $parameters['name'];
137     $machine_name = $parameters['type'];
138     $edit = array(
139       'label' => $label,
140       'id' => $machine_name,
141       'revision' => TRUE,
142     );
143     $this->drupalPostForm('admin/structure/block/block-content/types/add', $edit, t('Save'));
144     $this->assertText($label);
145   }
146
147 }