EntityEmbedTestBase::getTestFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 2
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Drupal\entity_embed\Tests;
4
5
use Drupal\Core\Entity\EntityInterface;
6
use Drupal\editor\Entity\Editor;
7
use Drupal\file\Entity\File;
8
use Drupal\filter\Entity\FilterFormat;
9
use Drupal\simpletest\WebTestBase;
10
11
/**
12
 * Base class for all entity_embed tests.
13
 */
14
abstract class EntityEmbedTestBase extends WebTestBase {
15
16
  /**
17
   * Modules to enable.
18
   *
19
   * @var array
20
   */
21
  public static $modules = ['entity_embed', 'entity_embed_test', 'node', 'ckeditor'];
22
23
  /**
24
   * The test user.
25
   *
26
   * @var \Drupal\user\UserInterface
27
   */
28
  protected $webUser;
29
30
  /**
31
   * A test node to be used for embedding.
32
   *
33
   * @var \Drupal\node\NodeInterface
34
   */
35
  protected $node;
36
37
  /**
38
   *
39
   */
40
  protected function setUp() {
41
    parent::setUp();
42
43
    // Create a page content type.
44
    $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
45
46
    // Create a text format and enable the entity_embed filter.
47
    $format = FilterFormat::create([
48
      'format' => 'custom_format',
49
      'name' => 'Custom format',
50
      'filters' => [
51
        'entity_embed' => [
52
          'status' => 1,
53
        ],
54
      ],
55
    ]);
56
    $format->save();
57
58
    $editor_group = [
59
      'name' => 'Entity Embed',
60
      'items' => [
61
        'node',
62
      ],
63
    ];
64
    $editor = Editor::create([
65
      'format' => 'custom_format',
66
      'editor' => 'ckeditor',
67
      'settings' => [
68
        'toolbar' => [
69
          'rows' => [[$editor_group]],
70
        ],
71
      ],
72
    ]);
73
    $editor->save();
74
75
    // Create a user with required permissions.
76
    $this->webUser = $this->drupalCreateUser([
77
      'access content',
78
      'create page content',
79
      'use text format custom_format',
80
    ]);
81
    $this->drupalLogin($this->webUser);
82
83
    // Create a sample node to be embedded.
84
    $settings = array();
85
    $settings['type'] = 'page';
86
    $settings['title'] = 'Embed Test Node';
87
    $settings['body'] = array('value' => 'This node is to be used for embedding in other nodes.', 'format' => 'custom_format');
88
    $this->node = $this->drupalCreateNode($settings);
89
  }
90
91
  /**
92
   * Retrieves a sample file of the specified type.
93
   *
94
   * @return \Drupal\file\FileInterface
95
   */
96
  protected function getTestFile($type_name, $size = NULL) {
97
    // Get a file to upload.
98
    $file = current($this->drupalGetTestFiles($type_name, $size));
99
100
    // Add a filesize property to files as would be read by
101
    // \Drupal\file\Entity\File::load().
102
    $file->filesize = filesize($file->uri);
103
104
    $file = File::create((array) $file);
105
    $file->save();
106
    return $file;
107
  }
108
109
  /**
110
   * Assert that the expected display plugins are available for the entity.
111
   */
112
  public function assertAvailableDisplayPlugins(EntityInterface $entity, array $expected_plugins, $message = '') {
113
    $plugin_options = $this->container->get('plugin.manager.entity_embed.display')
114
      ->getDefinitionOptionsForEntity($entity);
115
    $this->assertEqual([], array_diff($expected_plugins, array_keys($plugin_options)), $message);
116
  }
117
118
}
119