1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\Tests\url_embed\Functional; |
4
|
|
|
|
5
|
|
|
use Drupal\Component\Utility\Unicode; |
6
|
|
|
use Drupal\entity_test\Entity\EntityTest; |
7
|
|
|
use Drupal\link\LinkItemInterface; |
8
|
|
|
use Drupal\Tests\BrowserTestBase; |
9
|
|
|
use Drupal\url_embed\Tests\UrlEmbedTestBase; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Tests url_embed link field formatter. |
13
|
|
|
* |
14
|
|
|
* @group url_embed |
15
|
|
|
*/ |
16
|
|
|
class LinkEmbedFormatterTest extends BrowserTestBase { |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Modules to enable. |
20
|
|
|
* |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
public static $modules = ['url_embed', 'entity_test', 'link']; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* A field to use in this test class. |
27
|
|
|
* |
28
|
|
|
* @var \Drupal\field\Entity\FieldStorageConfig |
29
|
|
|
*/ |
30
|
|
|
protected $fieldStorage; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Tests the 'url_embed' formatter. |
34
|
|
|
*/ |
35
|
|
|
function testLinkEmbedFormatter() { |
|
|
|
|
36
|
|
|
$field_name = Unicode::strtolower($this->randomMachineName()); |
37
|
|
|
// Create a field with settings to validate. |
38
|
|
|
$this->fieldStorage = entity_create('field_storage_config', array( |
39
|
|
|
'field_name' => $field_name, |
40
|
|
|
'entity_type' => 'entity_test', |
41
|
|
|
'type' => 'link', |
42
|
|
|
'cardinality' => 2, |
43
|
|
|
)); |
44
|
|
|
$this->fieldStorage->save(); |
45
|
|
|
entity_create('field_config', array( |
46
|
|
|
'field_storage' => $this->fieldStorage, |
47
|
|
|
'bundle' => 'entity_test', |
48
|
|
|
'settings' => array( |
49
|
|
|
'title' => DRUPAL_OPTIONAL, |
50
|
|
|
'link_type' => LinkItemInterface::LINK_GENERIC, |
51
|
|
|
), |
52
|
|
|
))->save(); |
53
|
|
|
entity_get_form_display('entity_test', 'entity_test', 'default') |
54
|
|
|
->setComponent($field_name, array( |
55
|
|
|
'type' => 'link_default', |
56
|
|
|
)) |
57
|
|
|
->save(); |
58
|
|
|
$display_options = array( |
59
|
|
|
'type' => 'url_embed', |
60
|
|
|
'label' => 'hidden', |
61
|
|
|
); |
62
|
|
|
entity_get_display('entity_test', 'entity_test', 'full') |
63
|
|
|
->setComponent($field_name, $display_options) |
64
|
|
|
->save(); |
65
|
|
|
|
66
|
|
|
// Create an entity to test the embed formatter. |
67
|
|
|
$url = UrlEmbedTestBase::FLICKR_URL; |
68
|
|
|
$entity = EntityTest::create(); |
69
|
|
|
$entity->set($field_name, $url); |
70
|
|
|
$entity->save(); |
71
|
|
|
|
72
|
|
|
// Render the entity and verify that the link is output as an embed. |
73
|
|
|
$output = $this->renderTestEntity($entity->id()); |
74
|
|
|
$this->assertContains(UrlEmbedTestBase::FLICKR_OUTPUT_FIELD, $output); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Renders a test_entity and returns the output. |
79
|
|
|
* |
80
|
|
|
* @param int $id |
81
|
|
|
* The test_entity ID to render. |
82
|
|
|
* @param string $view_mode |
83
|
|
|
* (optional) The view mode to use for rendering. |
84
|
|
|
* @param bool $reset |
85
|
|
|
* (optional) Whether to reset the entity_test storage cache. Defaults to |
86
|
|
|
* TRUE to simplify testing. |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
* The rendered HTML output. |
90
|
|
|
*/ |
91
|
|
|
protected function renderTestEntity($id, $view_mode = 'full', $reset = TRUE) { |
92
|
|
|
if ($reset) { |
93
|
|
|
$this->container->get('entity_type.manager')->getStorage('entity_test')->resetCache([$id]); |
94
|
|
|
} |
95
|
|
|
$entity = EntityTest::load($id); |
96
|
|
|
$display = entity_get_display($entity->getEntityTypeId(), $entity->bundle(), $view_mode); |
97
|
|
|
$content = $display->build($entity); |
98
|
|
|
$output = \Drupal::service('renderer')->renderRoot($content); |
99
|
|
|
$output = (string) $output; |
100
|
|
|
$this->verbose($output); |
101
|
|
|
return $output; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.