Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\node\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
|
Chris@0
|
6 use Drupal\language\Entity\ConfigurableLanguage;
|
Chris@0
|
7 use Drupal\node\Entity\Node;
|
Chris@0
|
8 use Drupal\node\Entity\NodeType;
|
Chris@18
|
9 use Drupal\user\Entity\User;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Tests node owner functionality.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @group Entity
|
Chris@0
|
15 */
|
Chris@0
|
16 class NodeOwnerTest extends EntityKernelTestBase {
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Modules to enable.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @var array
|
Chris@0
|
22 */
|
Chris@0
|
23 public static $modules = ['node', 'language'];
|
Chris@0
|
24
|
Chris@0
|
25 protected function setUp() {
|
Chris@0
|
26 parent::setUp();
|
Chris@0
|
27
|
Chris@0
|
28 // Create the node bundles required for testing.
|
Chris@0
|
29 $type = NodeType::create([
|
Chris@0
|
30 'type' => 'page',
|
Chris@0
|
31 'name' => 'page',
|
Chris@0
|
32 ]);
|
Chris@0
|
33 $type->save();
|
Chris@0
|
34
|
Chris@0
|
35 // Enable two additional languages.
|
Chris@0
|
36 ConfigurableLanguage::createFromLangcode('de')->save();
|
Chris@0
|
37 ConfigurableLanguage::createFromLangcode('it')->save();
|
Chris@0
|
38
|
Chris@0
|
39 $this->installSchema('node', 'node_access');
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Tests node owner functionality.
|
Chris@0
|
44 */
|
Chris@0
|
45 public function testOwner() {
|
Chris@0
|
46 $user = $this->createUser();
|
Chris@0
|
47
|
Chris@0
|
48 $container = \Drupal::getContainer();
|
Chris@0
|
49 $container->get('current_user')->setAccount($user);
|
Chris@0
|
50
|
Chris@0
|
51 // Create a test node.
|
Chris@0
|
52 $english = Node::create([
|
Chris@0
|
53 'type' => 'page',
|
Chris@0
|
54 'title' => $this->randomMachineName(),
|
Chris@0
|
55 'language' => 'en',
|
Chris@0
|
56 ]);
|
Chris@0
|
57 $english->save();
|
Chris@0
|
58
|
Chris@0
|
59 $this->assertEqual($user->id(), $english->getOwnerId());
|
Chris@0
|
60
|
Chris@0
|
61 $german = $english->addTranslation('de');
|
Chris@0
|
62 $german->title = $this->randomString();
|
Chris@0
|
63 $italian = $english->addTranslation('it');
|
Chris@0
|
64 $italian->title = $this->randomString();
|
Chris@0
|
65
|
Chris@0
|
66 // Node::preSave() sets owner to anonymous user if owner is nor set.
|
Chris@0
|
67 $english->set('uid', ['target_id' => NULL]);
|
Chris@0
|
68 $german->set('uid', ['target_id' => NULL]);
|
Chris@0
|
69 $italian->set('uid', ['target_id' => NULL]);
|
Chris@0
|
70
|
Chris@0
|
71 // Entity::save() saves all translations!
|
Chris@0
|
72 $italian->save();
|
Chris@0
|
73
|
Chris@0
|
74 $this->assertEqual(0, $english->getOwnerId());
|
Chris@0
|
75 $this->assertEqual(0, $german->getOwnerId());
|
Chris@0
|
76 $this->assertEqual(0, $italian->getOwnerId());
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@18
|
79 /**
|
Chris@18
|
80 * Test an unsaved node owner.
|
Chris@18
|
81 */
|
Chris@18
|
82 public function testUnsavedNodeOwner() {
|
Chris@18
|
83 $user = User::create([
|
Chris@18
|
84 'name' => 'foo',
|
Chris@18
|
85 ]);
|
Chris@18
|
86 $node = Node::create([
|
Chris@18
|
87 'type' => 'page',
|
Chris@18
|
88 'title' => $this->randomMachineName(),
|
Chris@18
|
89 ]);
|
Chris@18
|
90 // Set the node owner while the user is unsaved and then immediately save
|
Chris@18
|
91 // the user and node.
|
Chris@18
|
92 $node->setOwner($user);
|
Chris@18
|
93 $user->save();
|
Chris@18
|
94 $node->save();
|
Chris@18
|
95
|
Chris@18
|
96 // The ID assigned to the newly saved user will now be the owner ID of the
|
Chris@18
|
97 // node.
|
Chris@18
|
98 $this->assertEquals($user->id(), $node->getOwnerId());
|
Chris@18
|
99 }
|
Chris@18
|
100
|
Chris@18
|
101 /**
|
Chris@18
|
102 * Tests the legacy method used as the default entity owner.
|
Chris@18
|
103 *
|
Chris@18
|
104 * @group legacy
|
Chris@18
|
105 * @expectedDeprecation The ::getCurrentUserId method is deprecated in 8.6.x and will be removed before 9.0.0.
|
Chris@18
|
106 */
|
Chris@18
|
107 public function testGetCurrentUserId() {
|
Chris@18
|
108 $this->assertEquals(['0'], Node::getCurrentUserId());
|
Chris@18
|
109 }
|
Chris@18
|
110
|
Chris@0
|
111 }
|