Completed
Push — 8.x-1.x ( 5a6226...1d49e2 )
by Janez
05:03
created

testEmbedButtonEditorAccessCheck()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 65
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 2
Metric Value
c 3
b 0
f 2
dl 0
loc 65
rs 9.3571
cc 1
eloc 47
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @file
5
 * Contains \Drupal\embed\Tests\EmbedButtonEditorAccessCheckTest.
6
 */
7
8
namespace Drupal\embed\Tests;
9
10
use Drupal\editor\Entity\Editor;
11
12
/**
13
 * Tests EmbedButtonEditorAccessCheck
14
 *
15
 * @group embed
16
 */
17
class EmbedButtonEditorAccessCheckTest extends EmbedTestBase {
18
19
  const SUCCESS = 'Success!';
20
21
  /**
22
   * Tests \Drupal\embed\Access\EmbedButtonEditorAccessCheck.
23
   */
24
  public function testEmbedButtonEditorAccessCheck() {
25
    // The anonymous user should have access to the plain_text format, but it
26
    // hasn't been configured to use an editor yet.
27
    $this->getRoute('plain_text', 'embed_test_default');
28
    $this->assertResponse(404);
29
    $this->assertCacheContext('route');
30
    $this->assertNoCacheTag('config:editor.editor.embed_test');
31
    $this->assertNoCacheTag('config:embed.button.embed_test_default');
32
33
    // The anonymous user should not have permission to use embed_test format.
34
    $this->getRoute('embed_test', 'embed_test_default');
35
    $this->assertResponse(403);
36
    $this->assertCacheContext('route');
37
    $this->assertNoCacheTag('config:editor.editor.embed_test');
38
    $this->assertNoCacheTag('config:embed.button.embed_test_default');
39
40
    // Now login a user that can use the embed_test format.
41
    $this->drupalLogin($this->webUser);
42
43
    $this->getRoute('plain_text', 'embed_test_default');
44
    $this->assertResponse(404);
45
    $this->assertCacheContext('route');
46
    $this->assertNoCacheTag('config:editor.editor.plain_text');
47
    $this->assertNoCacheTag('config:embed.button.embed_test_default');
48
49
    // Add an empty configuration for the plain_text editor configuration.
50
    $editor = Editor::create([
51
      'format' => 'plain_text',
52
      'editor' => 'ckeditor',
53
    ]);
54
    $editor->save();
55
    $this->getRoute('plain_text', 'embed_test_default');
56
    $this->assertResponse(403);
57
    $this->assertCacheContext('route');
58
    $this->assertCacheTag('config:editor.editor.plain_text');
59
    $this->assertCacheTag('config:embed.button.embed_test_default');
60
61
    $this->getRoute('embed_test', 'embed_test_default');
62
    $this->assertResponse(200);
63
    $this->assertCacheContext('route');
64
    $this->assertCacheTag('config:editor.editor.embed_test');
65
    $this->assertCacheTag('config:embed.button.embed_test_default');
66
    $this->assertText(static::SUCCESS);
67
68
    // Test route with an empty request.
69
    $this->getRoute('embed_test', 'embed_test_default', '');
70
    $this->assertResponse(404);
71
    $this->assertCacheContext('route');
72
    $this->assertCacheTag('config:editor.editor.embed_test');
73
    $this->assertCacheTag('config:embed.button.embed_test_default');
74
75
    // Test route with an invalid text format.
76
    $this->getRoute('invalid_editor', 'embed_test_default');
77
    $this->assertResponse(404);
78
    $this->assertCacheContext('route');
79
    $this->assertNoCacheTag('config:editor.editor.invalid_editor');
80
    $this->assertNoCacheTag('config:embed.button.embed_test_default');
81
82
    // Test route with an invalid embed button.
83
    $this->getRoute('embed_test', 'invalid_button');
84
    $this->assertResponse(404);
85
    $this->assertCacheContext('route');
86
    $this->assertNoCacheTag('config:editor.editor.embed_test');
87
    $this->assertNoCacheTag('config:embed.button.invalid_button');
88
  }
89
90
  /**
91
   * Performs a request to the embed_test.test_access route.
92
   *
93
   * @param string $editor_id
94
   *   ID of the editor.
95
   * @param string $embed_button_id
96
   *   ID of the embed button.
97
   * @param string $value
98
   *   The query string value to include.
99
   *
100
   * @return string
101
   *   The retrieved HTML string.
102
   */
103 View Code Duplication
  public function getRoute($editor_id, $embed_button_id, $value = NULL) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
104
    $url = 'embed-test/access/' . $editor_id . '/' . $embed_button_id;
105
    if (!isset($value)) {
106
      $value = static::SUCCESS;
107
    }
108
    return $this->drupalGet($url, ['query' => ['value' => $value]]);
109
  }
110
111
}
112