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

NodeTest::testNodeCreateDelete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
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\node\Entity\NodeType;
8
9
/**
10
 * Tests the driver's handling of node entities.
11
 *
12
 * @group driver
13
 */
14
class NodeTest extends DriverEntityKernelTestBase
15
{
16
17
  /**
18
   * {@inheritdoc}
19
   */
20
    public static $modules = ['node',];
21
22
  /**
23
   * Machine name of the entity type being tested.
24
   *
25
   * @string
26
   */
27
    protected $entityType = 'node';
28
29
    protected function setUp()
30
    {
31
        parent::setUp();
32
        $type = NodeType::create(['type' => 'article', 'name' => 'article']);
33
        $type->save();
34
35
        // Add a body field to articles.
36
        $this->installConfig('node');
37
        node_add_body_field($type);
38
39
        // Without node_access an error is thrown on deletion.
40
        $this->installSchema('node', 'node_access');
41
    }
42
43
44
  /**
45
   * Test that a node can be created and deleted.
46
   */
47
    public function testNodeCreateDelete()
48
    {
49
        $title = $this->driver->getRandom()->string();
50
        $node = (object) [
51
        'title' => $title,
52
        'type' => 'article',
53
        ];
54
        $node = $this->driver->createNode($node);
55
56
        $entities = $this->storage->loadByProperties(['title' => $title]);
57
        $this->assertEquals(1, count($entities));
58
59
        // Check the id of the new node has been added to the returned object.
60
        $entity = reset($entities);
61
        $this->assertEquals($entity->id(), $node->nid);
62
63
        // Check the node can be deleted.
64
        $this->driver->nodeDelete($node);
65
        $entities = $this->storage->loadByProperties(['title' => $title]);
66
        $this->assertEquals(0, count($entities));
67
    }
68
69
  /**
70
   * Test that a node can be created specifying its author by name.
71
   */
72 View Code Duplication
    public function testNodeCreateWithAuthorName()
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...
73
    {
74
        $title = $this->randomString();
75
        $author = $this->createUser();
76
        $authorName = $author->getUsername();
77
        $node = (object) [
78
        'title' => $title,
79
        'type' => 'article',
80
        'author' => $authorName,
81
        ];
82
        $node = $this->driver->createNode($node);
0 ignored issues
show
Unused Code introduced by
$node 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(['title' => $title]);
85
        $this->assertEquals(1, count($entities));
86
        $entity = reset($entities);
87
        $this->assertEquals($author->id(), $entity->getOwnerId());
88
    }
89
90
  /**
91
   * Test that a node can be created specifying its body field.
92
   */
93 View Code Duplication
    public function testNodeCreateWithBody()
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
    {
95
        $title = $this->randomString();
96
        $body = $this->randomString();
97
        $node = (object) [
98
        'title' => $title,
99
        'type' => 'article',
100
        'body' => $body,
101
        ];
102
        $node = $this->driver->createNode($node);
0 ignored issues
show
Unused Code introduced by
$node 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...
103
104
        $entities = $this->storage->loadByProperties(['title' => $title]);
105
        $this->assertEquals(1, count($entities));
106
        $entity = reset($entities);
107
        $this->assertEquals($body, $entity->get('body')->value);
108
    }
109
110
  /**
111
   * Test that a node can be created and deleted.
112
   */
113 View Code Duplication
    public function testNodeCreateDeleteByWrapper()
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...
114
    {
115
        $title = $this->driver->getRandom()->string();
116
        $fields = [
117
        'title' => $title,
118
        'type' => 'article',
119
        ];
120
        $node = DriverEntityDrupal8::create($fields, $this->entityType)->save();
121
122
        $entities = $this->storage->loadByProperties(['title' => $title]);
123
        $this->assertEquals(1, count($entities));
124
125
        // Check the id of the new node has been added to the returned object.
126
        $entity = reset($entities);
127
        $this->assertEquals($entity->id(), $node->nid);
0 ignored issues
show
Documentation introduced by
The property nid 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...
128
129
        // Check the node can be deleted.
130
        $this->driver->nodeDelete($node);
131
        $entities = $this->storage->loadByProperties(['title' => $title]);
132
        $this->assertEquals(0, count($entities));
133
    }
134
135
  /**
136
   * Test that a node can be created specifying its author by name.
137
   */
138 View Code Duplication
    public function testNodeCreateWithAuthorNameByWrapper()
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...
139
    {
140
        $title = $this->randomString();
141
        $author = $this->createUser();
142
        $authorName = $author->getUsername();
143
        $fields = [
144
        'title' => $title,
145
        'type' => 'article',
146
        'author' => $authorName,
147
        ];
148
        $node = DriverEntityDrupal8::create($fields, $this->entityType)->save();
0 ignored issues
show
Unused Code introduced by
$node 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...
149
150
        $entities = $this->storage->loadByProperties(['title' => $title]);
151
        $this->assertEquals(1, count($entities));
152
        $entity = reset($entities);
153
        $this->assertEquals($author->id(), $entity->getOwnerId());
154
    }
155
156
  /**
157
   * Test that a node can be created specifying its body field.
158
   */
159 View Code Duplication
    public function testNodeCreateWithBodyByWrapper()
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...
160
    {
161
        $title = $this->randomString();
162
        $body = $this->randomString();
163
        $fields = [
164
        'title' => $title,
165
        'type' => 'article',
166
        'body' => $body,
167
        ];
168
        $node = DriverEntityDrupal8::create($fields, $this->entityType)->save();
0 ignored issues
show
Unused Code introduced by
$node 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...
169
170
        $entities = $this->storage->loadByProperties(['title' => $title]);
171
        $this->assertEquals(1, count($entities));
172
        $entity = reset($entities);
173
        $this->assertEquals($body, $entity->get('body')->value);
174
    }
175
176
  /**
177
   * Test the created and changed fields on a node.
178
   */
179
  public function testNodeCreatedChanged()
180
  {
181
    $title = $this->randomString();
182
    $fields = [
183
      'title' => $title,
184
      'type' => 'article',
185
      'created' => '04/27/2013 11:11am UTC',
186
      'changed' => '07/27/2014 12:03pm UTC',
187
    ];
188
    $node = DriverEntityDrupal8::create($fields, $this->entityType)->save();
0 ignored issues
show
Unused Code introduced by
$node 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...
189
190
    $entities = $this->storage->loadByProperties(['title' => $title]);
191
    $this->assertEquals(1, count($entities));
192
    $entity = reset($entities);
193
    $this->assertEquals('1367061060', $entity->get('created')->value);
194
    $this->assertEquals('1406462580', $entity->get('changed')->value);
195
  }
196
}
197