forked from drupal/drupal
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Changed naming to Owner instead of Author, implemented ER module test…
… case.
- Loading branch information
Showing
8 changed files
with
103 additions
and
89 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\Core\Field\OwnerFieldDefinition. | ||
*/ | ||
|
||
namespace Drupal\Core\Field; | ||
|
||
use Drupal\Component\Utility\String; | ||
use Drupal\Core\Entity\EntityInterface; | ||
use Drupal\Core\Field\FieldDefinition; | ||
|
||
/** | ||
* Field definition which provides the current user as default value. | ||
*/ | ||
class OwnerFieldDefinition extends FieldDefinition { | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getDefaultValue(EntityInterface $entity) { | ||
return \Drupal::currentUser()->id(); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create($type = 'entity_reference') { | ||
if ($type != 'entity_reference') { | ||
throw new \InvalidArgumentException(String::format_string('The type for owner field definitions must be entity_reference, but @type was passed.', array('@type' => $type))); | ||
} | ||
$definition = parent::create($type); | ||
$definition->setSetting('target_type', 'user'); | ||
return $definition; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 0 additions & 42 deletions
42
core/modules/node/lib/Drupal/node/Tests/NodeAuthorDefaultTest.php
This file was deleted.
Oops, something went wrong.
54 changes: 54 additions & 0 deletions
54
core/modules/node/lib/Drupal/node/Tests/NodeOwnerDefaultTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains \Drupal\node\Tests\NodeOwnerDefaultTest. | ||
*/ | ||
|
||
namespace Drupal\node\Tests; | ||
|
||
use Drupal\system\Tests\Entity\EntityUnitTestBase; | ||
|
||
class NodeOwnerDefaultTest extends EntityUnitTestBase { | ||
|
||
/** | ||
* Modules to enable. | ||
* | ||
* @var array | ||
*/ | ||
public static $modules = array('node'); | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function getInfo() { | ||
return array( | ||
'name' => 'Node owner default', | ||
'description' => 'Tests the node uid field default value.', | ||
'group' => 'Node', | ||
); | ||
} | ||
|
||
/** | ||
* Tests that a created node has the current user set as author. | ||
*/ | ||
public function testOwnerDefault() { | ||
$user = $this->createUser(); | ||
$this->container->set('current_user', $user); | ||
$node = entity_create('node', array('type' => 'page', 'title' => 'test')); | ||
$this->assertEqual($node->getOwnerId(), $user->id(), 'Node owner is the current user.'); | ||
} | ||
|
||
/** | ||
* Tests that the current user is set as author when entity_reference module | ||
* is enabled. | ||
*/ | ||
public function testOwnerDefaultWithER() { | ||
$this->container->get('module_handler')->install(array('entity_reference')); | ||
$user = $this->createUser(); | ||
$this->container->set('current_user', $user); | ||
$node = entity_create('node', array('type' => 'page', 'title' => 'test')); | ||
$this->assertEqual($node->getOwnerId(), $user->id(), 'Node owner is the current user.'); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters