Completed
Pull Request — 8.x-1.x (#18)
by Janez
02:53 queued 15s
created

EmbedButtonEditorAccessCheckTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 95
Duplicated Lines 7.37 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 3
Bugs 0 Features 2
Metric Value
wmc 3
c 3
b 0
f 2
lcom 1
cbo 1
dl 7
loc 95
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testEmbedButtonEditorAccessCheck() 0 65 1
A getRoute() 7 7 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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