Skip to content

Commit

Permalink
Changed naming to Owner instead of Author, implemented ER module test…
Browse files Browse the repository at this point in the history
… case.
  • Loading branch information
klausi committed Mar 24, 2014
1 parent 46fb28d commit a32cd1d
Show file tree
Hide file tree
Showing 8 changed files with 103 additions and 89 deletions.
35 changes: 0 additions & 35 deletions core/lib/Drupal/Core/Field/AuthorFieldDefinition.php

This file was deleted.

38 changes: 38 additions & 0 deletions core/lib/Drupal/Core/Field/OwnerFieldDefinition.php
@@ -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;
}

}
7 changes: 3 additions & 4 deletions core/modules/comment/lib/Drupal/comment/Entity/Comment.php
Expand Up @@ -12,8 +12,8 @@
use Drupal\comment\CommentInterface;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\AuthorFieldDefinition;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\Field\OwnerFieldDefinition;
use Drupal\Core\Language\Language;
use Drupal\Core\TypedData\DataDefinition;
use Drupal\user\UserInterface;
Expand Down Expand Up @@ -238,10 +238,9 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDescription(t('The comment title or subject.'))
->setSetting('max_length', 64);

$fields['uid'] = AuthorFieldDefinition::create('entity_reference')
$fields['uid'] = OwnerFieldDefinition::create()
->setLabel(t('User ID'))
->setDescription(t('The user ID of the comment author.'))
->setSetting('target_type', 'user');
->setDescription(t('The user ID of the comment author.'));

$fields['name'] = FieldDefinition::create('string')
->setLabel(t('Name'))
Expand Down
6 changes: 3 additions & 3 deletions core/modules/file/lib/Drupal/file/Entity/File.php
Expand Up @@ -11,6 +11,7 @@
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\Field\OwnerFieldDefinition;
use Drupal\Core\Language\Language;
use Drupal\file\FileInterface;
use Drupal\user\UserInterface;
Expand Down Expand Up @@ -244,10 +245,9 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setLabel(t('Language code'))
->setDescription(t('The file language code.'));

$fields['uid'] = FieldDefinition::create('entity_reference')
$fields['uid'] = OwnerFieldDefinition::create()
->setLabel(t('User ID'))
->setDescription(t('The user ID of the file.'))
->setSetting('target_type', 'user');
->setDescription(t('The user ID of the file.'));

$fields['filename'] = FieldDefinition::create('string')
->setLabel(t('Filename'))
Expand Down
7 changes: 3 additions & 4 deletions core/modules/node/lib/Drupal/node/Entity/Node.php
Expand Up @@ -10,8 +10,8 @@
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\AuthorFieldDefinition;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\Field\OwnerFieldDefinition;
use Drupal\Core\Language\Language;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\NodeInterface;
Expand Down Expand Up @@ -378,10 +378,9 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
))
->setDisplayConfigurable('form', TRUE);

$fields['uid'] = AuthorFieldDefinition::create('entity_reference')
$fields['uid'] = OwnerFieldDefinition::create()
->setLabel(t('User ID'))
->setDescription(t('The user ID of the node author.'))
->setSetting('target_type', 'user');
->setDescription(t('The user ID of the node author.'));

$fields['status'] = FieldDefinition::create('boolean')
->setLabel(t('Publishing status'))
Expand Down
42 changes: 0 additions & 42 deletions core/modules/node/lib/Drupal/node/Tests/NodeAuthorDefaultTest.php

This file was deleted.

54 changes: 54 additions & 0 deletions core/modules/node/lib/Drupal/node/Tests/NodeOwnerDefaultTest.php
@@ -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.');
}

}
Expand Up @@ -10,6 +10,7 @@
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\FieldDefinition;
use Drupal\Core\Field\OwnerFieldDefinition;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Language\Language;
use Drupal\user\EntityOwnerInterface;
Expand Down Expand Up @@ -136,7 +137,7 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDescription(t('The bundle of the test entity.'))
->setRequired(TRUE);

$fields['user_id'] = FieldDefinition::create('entity_reference')
$fields['user_id'] = OwnerFieldDefinition::create()
->setLabel(t('User ID'))
->setDescription(t('The ID of the associated user.'))
->setSettings(array('target_type' => 'user'))
Expand Down

0 comments on commit a32cd1d

Please sign in to comment.