Completed
Pull Request — master (#157)
by
unknown
01:46
created

TaxonomyTermTest::testTermCreateDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
dl 21
loc 21
rs 9.3142
c 0
b 0
f 0
cc 1
eloc 13
nc 1
nop 0
1
<?php
2
3
namespace Drupal\Tests\Driver\Kernel\Drupal8\Entity;
4
5
use Drupal\Driver\Wrapper\Entity\DriverEntityDrupal8;
6
use Drupal\Tests\Driver\Kernel\Drupal8\Entity\DriverEntityKernelTestBase;
7
use Drupal\taxonomy\Entity\Vocabulary;
8
use Drupal\taxonomy\Entity\Term;
9
10
/**
11
 * Tests the driver's handling of term entities.
12
 *
13
 * @group driver
14
 */
15
class TaxonomyTermTest extends DriverEntityKernelTestBase
16
{
17
18
  /**
19
   * {@inheritdoc}
20
   */
21
    public static $modules = ['taxonomy',];
22
23
  /**
24
   * Machine name of the entity type being tested.
25
   *
26
   * @string
27
   */
28
    protected $entityType = 'taxonomy_term';
29
30
    protected function setUp()
31
    {
32
        parent::setUp();
33
        $this->installEntitySchema('taxonomy_term');
34
        $vocabulary = Vocabulary::create(['vid' => 'testvocab', 'name' => 'test vocabulary']);
35
        $vocabulary->save();
36
    }
37
38
  /**
39
   * Test that a term can be created and deleted.
40
   */
41 View Code Duplication
    public function testTermCreateDelete()
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...
42
    {
43
        $name = $this->randomString();
44
        $term = (object) [
45
        'name' => $name,
46
        'vocabulary_machine_name' => 'testvocab',
47
        ];
48
        $term = $this->driver->createTerm($term);
49
50
        $entities = $this->storage->loadByProperties(['name' => $name]);
51
        $this->assertEquals(1, count($entities));
52
53
        // Check the id of the new term has been added to the returned object.
54
        $entity = reset($entities);
55
        $this->assertEquals($entity->id(), $term->tid);
56
57
        // Check the term can be deleted.
58
        $this->driver->termDelete($term);
59
        $entities = $this->storage->loadByProperties(['name' => $name]);
60
        $this->assertEquals(0, count($entities));
61
    }
62
63
  /**
64
   * Test that a term can be created with a parent term.
65
   */
66
    public function testTermCreateWithParent()
67
    {
68
      return;
69
        $parentName = $this->randomString();
0 ignored issues
show
Unused Code introduced by
$parentName = $this->randomString(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
70
        $parent = (object) [
71
        'name' => $parentName,
72
        'vocabulary_machine_name' => 'testvocab',
73
        ];
74
        $parent = $this->driver->createTerm($parent);
75
76
        $childName = $this->randomString();
77
        $child = (object) [
78
        'name' => $childName,
79
        'vocabulary_machine_name' => 'testvocab',
80
        'parent' => $parentName,
81
        ];
82
        $child = $this->driver->createTerm($child);
0 ignored issues
show
Unused Code introduced by
$child 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...
83
84
        $entities = $this->storage->loadByProperties(['name' => $childName]);
85
        $this->assertEquals(1, count($entities));
86
87
        // Check the parent is set on the child term.
88
        $entity = reset($entities);
89
        $parentEntities = $this->storage->loadParents($entity->id());
90
        $parentEntity = reset($parentEntities);
91
        $this->assertEquals($parent->tid, $parentEntity->id());
92
    }
93
94
  /**
95
   * Test that a term can be created and deleted.
96
   */
97 View Code Duplication
    public function testTermCreateDeleteByWrapper()
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...
98
    {
99
      return;
100
        $name = $this->randomString();
0 ignored issues
show
Unused Code introduced by
$name = $this->randomString(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
101
        $fields = [
102
        'name' => $name,
103
        'vocabulary' => 'testvocab',
104
        ];
105
        $term = DriverEntityDrupal8::create($fields, $this->entityType)->save();
106
107
        $entities = $this->storage->loadByProperties(['name' => $name]);
108
        $this->assertEquals(1, count($entities));
109
110
        // Check the id of the new term has been added to the returned object.
111
        $entity = reset($entities);
112
        $this->assertEquals($entity->id(), $term->tid);
113
114
        // Check the term can be deleted.
115
        $term->delete();
116
        $entities = $this->storage->loadByProperties(['name' => $name]);
117
        $this->assertEquals(0, count($entities));
118
    }
119
120
  /**
121
   * Test that a term can be created with a parent term.
122
   * Also that a vocabulary can be referred to by it label.
123
   */
124
    public function testTermCreateWithParentByWrapper()
125
    {
126
      return;
127
        $parentName = $this->randomString();
0 ignored issues
show
Unused Code introduced by
$parentName = $this->randomString(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
128
        $parentFields = [
129
        'name' => $parentName,
130
        // Test using label not machine name for vocab reference.
131
        'vocabulary' => 'test vocabulary',
132
        ];
133
        $parent = DriverEntityDrupal8::create($parentFields, $this->entityType)->save();
134
135
        $childName = $this->randomString();
136
        $childFields = [
137
        'name' => $childName,
138
        'vocabulary' => 'testvocab',
139
        'parent' => $parentName,
140
        ];
141
        $child = DriverEntityDrupal8::create($childFields, $this->entityType)->save();
0 ignored issues
show
Unused Code introduced by
$child 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...
142
143
        $entities = $this->storage->loadByProperties(['name' => $childName]);
144
        $this->assertEquals(1, count($entities));
145
146
        // Check the parent is set on the child term.
147
        $entity = reset($entities);
148
        $parentEntities = $this->storage->loadParents($entity->id());
149
        $parentEntity = reset($parentEntities);
150
        $this->assertEquals($parent->tid, $parentEntity->id());
151
    }
152
153
  /**
154
   * Test 'vocabulary_machine_name' as BC support for old human-friendly name.
155
   */
156
    public function testVocabularyBCBundleName()
157
    {
158
      return;
159
        $name = $this->randomString();
0 ignored issues
show
Unused Code introduced by
$name = $this->randomString(); does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
160
        $fields = [
161
        'name' => $name,
162
        'vocabulary_machine_name' => 'testvocab',
163
        ];
164
        $term = DriverEntityDrupal8::create($fields, $this->entityType)->save();
0 ignored issues
show
Unused Code introduced by
$term 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...
165
166
        $entities = $this->storage->loadByProperties(['name' => $name, 'vid' => 'testvocab']);
167
        $this->assertEquals(1, count($entities));
168
    }
169
}
170