PreviewTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 57
Duplicated Lines 12.28 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 7
loc 57
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 1

2 Methods

Rating   Name   Duplication   Size   Complexity  
B testPreviewRoute() 0 29 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
namespace Drupal\embed\Tests;
4
5
/**
6
 * Tests the preview controller and route.
7
 *
8
 * @group embed
9
 */
10
class PreviewTest extends EmbedTestBase {
11
12
  const SUCCESS = 'Success!';
13
14
  /**
15
   * Tests the route used for generating preview of embedding entities.
16
   */
17
  public function testPreviewRoute() {
18
    // Ensure the default filter can be previewed by the anonymous user.
19
    $this->getRoute('plain_text');
20
    $this->assertResponse(200);
21
    $this->assertText(static::SUCCESS);
22
23
    // The anonymous user should not have permission to use embed_test format.
24
    $this->getRoute('embed_test');
25
    $this->assertResponse(403);
26
27
    // Now login a user that can use the embed_test format.
28
    $this->drupalLogin($this->webUser);
29
30
    $this->getRoute('plain_text');
31
    $this->assertResponse(200);
32
    $this->assertText(static::SUCCESS);
33
34
    $this->getRoute('embed_test');
35
    $this->assertResponse(200);
36
    $this->assertText(static::SUCCESS);
37
38
    // Test preview route with an empty request.
39
    $this->getRoute('embed_test', '');
40
    $this->assertResponse(404);
41
42
    // Test preview route with an invalid text format.
43
    $this->getRoute('invalid_format');
44
    $this->assertResponse(404);
45
  }
46
47
  /**
48
   * Performs a request to the embed.preview route.
49
   *
50
   * @param string $filter_format_id
51
   *   ID of the filter format.
52
   * @param string $value
53
   *   The query string value to include.
54
   *
55
   * @return string
56
   *   The retrieved HTML string.
57
   */
58 View Code Duplication
  public function getRoute($filter_format_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...
59
    $url = 'embed/preview/' . $filter_format_id;
60
    if (!isset($value)) {
61
      $value = static::SUCCESS;
62
    }
63
    return $this->drupalGet($url, ['query' => ['value' => $value]]);
64
  }
65
66
}
67