Completed
Pull Request — master (#157)
by
unknown
12:36
created

TaxonomyTermTest::testVocabularyBcBundleName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
c 0
b 0
f 0
rs 9.4285
cc 1
eloc 8
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\taxonomy\Entity\Vocabulary;
7
8
/**
9
 * Tests the driver's handling of term entities.
10
 *
11
 * @group driver
12
 */
13
class TaxonomyTermTest extends DriverEntityKernelTestBase {
14
15
  /**
16
   * {@inheritdoc}
17
   */
18
  public static $modules = ['taxonomy'];
19
20
  /**
21
   * Machine name of the entity type being tested.
22
   *
23
   * @var string
24
   */
25
  protected $entityType = 'taxonomy_term';
26
27
  /**
28
   * {@inheritdoc}
29
   */
30
  protected function setUp() {
31
    parent::setUp();
32
    $this->installEntitySchema('taxonomy_term');
33
    $vocabulary = Vocabulary::create(['vid' => 'testvocab', 'name' => 'test vocabulary']);
34
    $vocabulary->save();
35
  }
36
37
  /**
38
   * Test that a term can be created and deleted.
39
   */
40 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...
41
    $name = $this->randomString();
42
    $term = (object) [
43
      'name' => $name,
44
      'vocabulary_machine_name' => 'testvocab',
45
    ];
46
    $term = $this->driver->createTerm($term);
47
48
    $entities = $this->storage->loadByProperties(['name' => $name]);
49
    $this->assertEquals(1, count($entities));
50
51
    // Check the id of the new term has been added to the returned object.
52
    $entity = reset($entities);
53
    $this->assertEquals($entity->id(), $term->tid);
54
55
    // Check the term can be deleted.
56
    $this->driver->termDelete($term);
57
    $entities = $this->storage->loadByProperties(['name' => $name]);
58
    $this->assertEquals(0, count($entities));
59
  }
60
61
  /**
62
   * Test that a term can be created with a parent term.
63
   */
64
  public function testTermCreateWithParent() {
65
    $parentName = $this->randomString();
66
    $parent = (object) [
67
      'name' => $parentName,
68
      'vocabulary_machine_name' => 'testvocab',
69
    ];
70
    $parent = $this->driver->createTerm($parent);
71
72
    $childName = $this->randomString();
73
    $child = (object) [
74
      'name' => $childName,
75
      'vocabulary_machine_name' => 'testvocab',
76
      'parent' => $parentName,
77
    ];
78
    $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...
79
80
    $entities = $this->storage->loadByProperties(['name' => $childName]);
81
    $this->assertEquals(1, count($entities));
82
83
    // Check the parent is set on the child term.
84
    $entity = reset($entities);
85
    $parentEntities = $this->storage->loadParents($entity->id());
86
    $parentEntity = reset($parentEntities);
87
    $this->assertEquals($parent->tid, $parentEntity->id());
88
  }
89
90
  /**
91
   * Test that a term can be created and deleted.
92
   */
93 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...
94
    $name = $this->randomString();
95
    $fields = [
96
      'name' => $name,
97
      'vocabulary' => 'testvocab',
98
    ];
99
    $term = DriverEntityDrupal8::create($fields, $this->entityType)->save();
100
101
    $entities = $this->storage->loadByProperties(['name' => $name]);
102
    $this->assertEquals(1, count($entities));
103
104
    // Check the id of the new term has been added to the returned object.
105
    $entity = reset($entities);
106
    $this->assertEquals($entity->id(), $term->tid);
0 ignored issues
show
Documentation introduced by
The property tid does not exist on object<Drupal\Driver\Wra...ntity\DriverEntityBase>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
107
108
    // Check the term can be deleted.
109
    $term->delete();
110
    $entities = $this->storage->loadByProperties(['name' => $name]);
111
    $this->assertEquals(0, count($entities));
112
  }
113
114
  /**
115
   * Test that a term can be created with a parent term.
116
   *
117
   * Also tests that a vocabulary can be referred to by it label.
118
   */
119
  public function testTermCreateWithParentByWrapper() {
120
    $parentName = $this->randomString();
121
    $parentFields = [
122
      'name' => $parentName,
123
        // Test using label not machine name for vocab reference.
124
      'vocabulary' => 'test vocabulary',
125
    ];
126
    $parent = DriverEntityDrupal8::create($parentFields, $this->entityType)->save();
127
128
    $childName = $this->randomString();
129
    $childFields = [
130
      'name' => $childName,
131
      'vocabulary' => 'testvocab',
132
      'parent' => $parentName,
133
    ];
134
    $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...
135
136
    $entities = $this->storage->loadByProperties(['name' => $childName]);
137
    $this->assertEquals(1, count($entities));
138
139
    // Check the parent is set on the child term.
140
    $entity = reset($entities);
141
    $parentEntities = $this->storage->loadParents($entity->id());
142
    $parentEntity = reset($parentEntities);
143
    $this->assertEquals($parent->tid, $parentEntity->id());
0 ignored issues
show
Documentation introduced by
The property tid does not exist on object<Drupal\Driver\Wra...ntity\DriverEntityBase>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
144
  }
145
146
  /**
147
   * Test 'vocabulary_machine_name' as BC support for old human-friendly name.
148
   */
149
  public function testVocabularyBcBundleName() {
150
    $name = $this->randomString();
151
    $fields = [
152
      'name' => $name,
153
      'vocabulary_machine_name' => 'testvocab',
154
    ];
155
    $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...
156
157
    $entities = $this->storage->loadByProperties(['name' => $name, 'vid' => 'testvocab']);
158
    $this->assertEquals(1, count($entities));
159
  }
160
161
}
162