Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / web / core / modules / node / tests / src / Kernel / NodeOwnerTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Kernel;
4
5 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
6 use Drupal\language\Entity\ConfigurableLanguage;
7 use Drupal\node\Entity\Node;
8 use Drupal\node\Entity\NodeType;
9
10 /**
11  * Tests node owner functionality.
12  *
13  * @group Entity
14  */
15 class NodeOwnerTest extends EntityKernelTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['node', 'language'];
23
24   protected function setUp() {
25     parent::setUp();
26
27     // Create the node bundles required for testing.
28     $type = NodeType::create([
29       'type' => 'page',
30       'name' => 'page',
31     ]);
32     $type->save();
33
34     // Enable two additional languages.
35     ConfigurableLanguage::createFromLangcode('de')->save();
36     ConfigurableLanguage::createFromLangcode('it')->save();
37
38     $this->installSchema('node', 'node_access');
39   }
40
41   /**
42    * Tests node owner functionality.
43    */
44   public function testOwner() {
45     $user = $this->createUser();
46
47     $container = \Drupal::getContainer();
48     $container->get('current_user')->setAccount($user);
49
50     // Create a test node.
51     $english = Node::create([
52       'type' => 'page',
53       'title' => $this->randomMachineName(),
54       'language' => 'en',
55     ]);
56     $english->save();
57
58     $this->assertEqual($user->id(), $english->getOwnerId());
59
60     $german = $english->addTranslation('de');
61     $german->title = $this->randomString();
62     $italian = $english->addTranslation('it');
63     $italian->title = $this->randomString();
64
65     // Node::preSave() sets owner to anonymous user if owner is nor set.
66     $english->set('uid', ['target_id' => NULL]);
67     $german->set('uid', ['target_id' => NULL]);
68     $italian->set('uid', ['target_id' => NULL]);
69
70     // Entity::save() saves all translations!
71     $italian->save();
72
73     $this->assertEqual(0, $english->getOwnerId());
74     $this->assertEqual(0, $german->getOwnerId());
75     $this->assertEqual(0, $italian->getOwnerId());
76   }
77
78 }