Chris@0: 'page', Chris@0: 'name' => 'page', Chris@0: ]); Chris@0: $type->save(); Chris@0: Chris@0: // Enable two additional languages. Chris@0: ConfigurableLanguage::createFromLangcode('de')->save(); Chris@0: ConfigurableLanguage::createFromLangcode('it')->save(); Chris@0: Chris@0: $this->installSchema('node', 'node_access'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests node owner functionality. Chris@0: */ Chris@0: public function testOwner() { Chris@0: $user = $this->createUser(); Chris@0: Chris@0: $container = \Drupal::getContainer(); Chris@0: $container->get('current_user')->setAccount($user); Chris@0: Chris@0: // Create a test node. Chris@0: $english = Node::create([ Chris@0: 'type' => 'page', Chris@0: 'title' => $this->randomMachineName(), Chris@0: 'language' => 'en', Chris@0: ]); Chris@0: $english->save(); Chris@0: Chris@0: $this->assertEqual($user->id(), $english->getOwnerId()); Chris@0: Chris@0: $german = $english->addTranslation('de'); Chris@0: $german->title = $this->randomString(); Chris@0: $italian = $english->addTranslation('it'); Chris@0: $italian->title = $this->randomString(); Chris@0: Chris@0: // Node::preSave() sets owner to anonymous user if owner is nor set. Chris@0: $english->set('uid', ['target_id' => NULL]); Chris@0: $german->set('uid', ['target_id' => NULL]); Chris@0: $italian->set('uid', ['target_id' => NULL]); Chris@0: Chris@0: // Entity::save() saves all translations! Chris@0: $italian->save(); Chris@0: Chris@0: $this->assertEqual(0, $english->getOwnerId()); Chris@0: $this->assertEqual(0, $german->getOwnerId()); Chris@0: $this->assertEqual(0, $italian->getOwnerId()); Chris@0: } Chris@0: Chris@18: /** Chris@18: * Test an unsaved node owner. Chris@18: */ Chris@18: public function testUnsavedNodeOwner() { Chris@18: $user = User::create([ Chris@18: 'name' => 'foo', Chris@18: ]); Chris@18: $node = Node::create([ Chris@18: 'type' => 'page', Chris@18: 'title' => $this->randomMachineName(), Chris@18: ]); Chris@18: // Set the node owner while the user is unsaved and then immediately save Chris@18: // the user and node. Chris@18: $node->setOwner($user); Chris@18: $user->save(); Chris@18: $node->save(); Chris@18: Chris@18: // The ID assigned to the newly saved user will now be the owner ID of the Chris@18: // node. Chris@18: $this->assertEquals($user->id(), $node->getOwnerId()); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Tests the legacy method used as the default entity owner. Chris@18: * Chris@18: * @group legacy Chris@18: * @expectedDeprecation The ::getCurrentUserId method is deprecated in 8.6.x and will be removed before 9.0.0. Chris@18: */ Chris@18: public function testGetCurrentUserId() { Chris@18: $this->assertEquals(['0'], Node::getCurrentUserId()); Chris@18: } Chris@18: Chris@0: }