1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Drupal\embed\Tests; |
4
|
|
|
|
5
|
|
|
use Drupal\editor\Entity\Editor; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Tests EmbedButtonEditorAccessCheck. |
9
|
|
|
* |
10
|
|
|
* @group embed |
11
|
|
|
*/ |
12
|
|
|
class EmbedButtonEditorAccessCheckTest extends EmbedTestBase { |
13
|
|
|
|
14
|
|
|
const SUCCESS = 'Success!'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Tests \Drupal\embed\Access\EmbedButtonEditorAccessCheck. |
18
|
|
|
*/ |
19
|
|
|
public function testEmbedButtonEditorAccessCheck() { |
20
|
|
|
// The anonymous user should have access to the plain_text format, but it |
21
|
|
|
// hasn't been configured to use an editor yet. |
22
|
|
|
$this->getRoute('plain_text', 'embed_test_default'); |
23
|
|
|
$this->assertResponse(404); |
24
|
|
|
$this->assertCacheContext('route'); |
25
|
|
|
$this->assertNoCacheTag('config:editor.editor.embed_test'); |
26
|
|
|
$this->assertNoCacheTag('config:embed.button.embed_test_default'); |
27
|
|
|
|
28
|
|
|
// The anonymous user should not have permission to use embed_test format. |
29
|
|
|
$this->getRoute('embed_test', 'embed_test_default'); |
30
|
|
|
$this->assertResponse(403); |
31
|
|
|
$this->assertCacheContext('route'); |
32
|
|
|
$this->assertNoCacheTag('config:editor.editor.embed_test'); |
33
|
|
|
$this->assertNoCacheTag('config:embed.button.embed_test_default'); |
34
|
|
|
|
35
|
|
|
// Now login a user that can use the embed_test format. |
36
|
|
|
$this->drupalLogin($this->webUser); |
37
|
|
|
|
38
|
|
|
$this->getRoute('plain_text', 'embed_test_default'); |
39
|
|
|
$this->assertResponse(404); |
40
|
|
|
$this->assertCacheContext('route'); |
41
|
|
|
$this->assertNoCacheTag('config:editor.editor.plain_text'); |
42
|
|
|
$this->assertNoCacheTag('config:embed.button.embed_test_default'); |
43
|
|
|
|
44
|
|
|
// Add an empty configuration for the plain_text editor configuration. |
45
|
|
|
$editor = Editor::create([ |
46
|
|
|
'format' => 'plain_text', |
47
|
|
|
'editor' => 'ckeditor', |
48
|
|
|
]); |
49
|
|
|
$editor->save(); |
50
|
|
|
$this->getRoute('plain_text', 'embed_test_default'); |
51
|
|
|
$this->assertResponse(403); |
52
|
|
|
$this->assertCacheContext('route'); |
53
|
|
|
$this->assertCacheTag('config:editor.editor.plain_text'); |
54
|
|
|
$this->assertCacheTag('config:embed.button.embed_test_default'); |
55
|
|
|
|
56
|
|
|
$this->getRoute('embed_test', 'embed_test_default'); |
57
|
|
|
$this->assertResponse(200); |
58
|
|
|
$this->assertCacheContext('route'); |
59
|
|
|
$this->assertCacheTag('config:editor.editor.embed_test'); |
60
|
|
|
$this->assertCacheTag('config:embed.button.embed_test_default'); |
61
|
|
|
$this->assertText(static::SUCCESS); |
62
|
|
|
|
63
|
|
|
// Test route with an empty request. |
64
|
|
|
$this->getRoute('embed_test', 'embed_test_default', ''); |
65
|
|
|
$this->assertResponse(404); |
66
|
|
|
$this->assertCacheContext('route'); |
67
|
|
|
$this->assertCacheTag('config:editor.editor.embed_test'); |
68
|
|
|
$this->assertCacheTag('config:embed.button.embed_test_default'); |
69
|
|
|
|
70
|
|
|
// Test route with an invalid text format. |
71
|
|
|
$this->getRoute('invalid_editor', 'embed_test_default'); |
72
|
|
|
$this->assertResponse(404); |
73
|
|
|
$this->assertCacheContext('route'); |
74
|
|
|
$this->assertNoCacheTag('config:editor.editor.invalid_editor'); |
75
|
|
|
$this->assertNoCacheTag('config:embed.button.embed_test_default'); |
76
|
|
|
|
77
|
|
|
// Test route with an invalid embed button. |
78
|
|
|
$this->getRoute('embed_test', 'invalid_button'); |
79
|
|
|
$this->assertResponse(404); |
80
|
|
|
$this->assertCacheContext('route'); |
81
|
|
|
$this->assertNoCacheTag('config:editor.editor.embed_test'); |
82
|
|
|
$this->assertNoCacheTag('config:embed.button.invalid_button'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Performs a request to the embed_test.test_access route. |
87
|
|
|
* |
88
|
|
|
* @param string $editor_id |
89
|
|
|
* ID of the editor. |
90
|
|
|
* @param string $embed_button_id |
91
|
|
|
* ID of the embed button. |
92
|
|
|
* @param string $value |
93
|
|
|
* The query string value to include. |
94
|
|
|
* |
95
|
|
|
* @return string |
96
|
|
|
* The retrieved HTML string. |
97
|
|
|
*/ |
98
|
|
View Code Duplication |
public function getRoute($editor_id, $embed_button_id, $value = NULL) { |
|
|
|
|
99
|
|
|
$url = 'embed-test/access/' . $editor_id . '/' . $embed_button_id; |
100
|
|
|
if (!isset($value)) { |
101
|
|
|
$value = static::SUCCESS; |
102
|
|
|
} |
103
|
|
|
return $this->drupalGet($url, ['query' => ['value' => $value]]); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.