Completed
Push — 8.x-1.x ( f0a765...802e84 )
by Janez
02:10
created

InstagramBundleTest   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 100
Duplicated Lines 17 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 17
loc 100
rs 10
wmc 3
lcom 1
cbo 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 15 1
A testMediaBundleCreationFromModule() 17 17 1
B testMediaBundleItemCreation() 0 25 1

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\media\Tests;
4
5
use Drupal\simpletest\WebTestBase;
6
7
/**
8
 * Ensures that media bundle for instagram can be created.
9
 *
10
 * @group media
11
 */
12
class InstagramBundleTest extends WebTestBase {
13
  /**
14
   * Exempt from strict schema checking.
15
   *
16
   * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker
17
   *
18
   * @var bool
19
   */
20
  protected $strictConfigSchema = FALSE;
21
22
  /**
23
   * Modules to enable.
24
   *
25
   * @var array
26
   */
27
  public static $modules = [
28
    'media',
29
    'media_entity',
30
    'media_entity_instagram',
31
    'link',
32
    'node',
33
  ];
34
35
  /**
36
   * The test media bundle.
37
   *
38
   * @var \Drupal\media_entity\MediaBundleInterface
39
   */
40
  protected $testBundle;
41
42
  /**
43
   * {@inheritdoc}
44
   */
45
  protected function setUp() {
46
    parent::setUp();
47
    $this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('instagram');
48
49
    $adminUser = $this->drupalCreateUser([
50
      'view media',
51
      'create media',
52
      'update media',
53
      'update any media',
54
      'delete media',
55
      'delete any media',
56
      'access media overview',
57
    ]);
58
    $this->drupalLogin($adminUser);
59
  }
60
61
  /**
62
   * Tests instagram media bundle creation from config files.
63
   */
64 View Code Duplication
  public function testMediaBundleCreationFromModule() {
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...
65
    $type_configuration = [
66
      'use_instagram_api' => FALSE,
67
      'source_field' => 'field_instagram_url',
68
      'client_id'  => '',
69
    ];
70
    $field_map = [
71
      'shortcode' => 'field_instagram_shortcode',
72
    ];
73
74
    $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.');
75
    $this->assertEqual($this->testBundle->get('label'), 'Instagram Post', 'Correct label detected.');
76
    $this->assertEqual($this->testBundle->get('description'), 'Use this to attach Instagram posts to your content.', 'Correct description detected.');
77
    $this->assertEqual($this->testBundle->get('type'), 'instagram', 'Correct plugin ID detected.');
78
    $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.');
79
    $this->assertEqual($this->testBundle->get('field_map'), $field_map, 'Correct field map detected.');
80
  }
81
82
  /**
83
   * Tests item creation and thumbnail.
84
   */
85
  public function testMediaBundleItemCreation() {
86
    // Define the media item name.
87
    $name = $this->randomMachineName();
88
    $instagram_url = 'https://www.instagram.com/p/C/';
89
    $edit = [
90
      'name[0][value]' => $name,
91
      'field_instagram_url[0][uri]' => $instagram_url,
92
    ];
93
94
    // Save the Instagram post.
95
    $this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish'));
96
97
    // Assert that the formatter exists on this page.
98
    $this->assertFieldByXPath('//iframe');
99
100
    // Let's retrieve the media id and corresponding media entity object.
101
    $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'instagram')->sort('created', 'DESC')->execute();
102
    $media_id = reset($media_id);
103
    /** @var \Drupal\media_entity\MediaInterface $media */
104
    $media = $this->container->get('entity_type.manager')
105
      ->getStorage('media')
106
      ->loadUnchanged($media_id);
107
    $properties = $media->toArray();
0 ignored issues
show
Unused Code introduced by
$properties is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
108
    $this->assertEqual($media->get('field_instagram_shortcode')[0]->getValue()['value'], "C", "Correct shortcode stored.");
109
  }
110
111
}
112