Issue #2349819 by amateescu, swentel, Leksat, larowlan, dawehner, Gábor Hojtsy, mpdonadio, benjy, jmuzz, yched, jibran, marthinal, dmsmidt: String field type doesn't consider empty string as empty value

8.0.x
effulgentsia 2015-08-20 12:15:48 -07:00
parent 8dc9763d2f
commit 764890d629
35 changed files with 135 additions and 23 deletions

View File

@ -63,4 +63,16 @@ class PasswordItem extends StringItem {
}
}
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
// We cannot use the parent implementation from StringItem as it does not
// consider the additional 'existing' property that PasswordItem contains.
$value = $this->get('value')->getValue();
$existing = $this->get('existing')->getValue();
return $value === NULL && $existing === NULL;
}
}

View File

@ -40,4 +40,12 @@ abstract class StringItemBase extends FieldItemBase {
return $properties;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')->getValue();
return $value === NULL || $value === '';
}
}

View File

@ -47,9 +47,10 @@ class FeedProcessorPluginTest extends AggregatorTestBase {
*/
public function testDelete() {
$feed = $this->createFeed();
$description = $feed->description->value ?: '';
$this->updateAndDelete($feed, NULL);
// Make sure the feed title is changed.
$entities = entity_load_multiple_by_properties('aggregator_feed', array('description' => $feed->description->value));
$entities = entity_load_multiple_by_properties('aggregator_feed', array('description' => $description));
$this->assertTrue(empty($entities));
}

View File

@ -57,7 +57,7 @@ class BookUninstallTest extends KernelTestBase {
$allowed_types[] = $content_type->id();
$book_config->set('allowed_types', $allowed_types)->save();
$node = Node::create(array('type' => $content_type->id()));
$node = Node::create(array('title' => $this->randomString(), 'type' => $content_type->id()));
$node->book['bid'] = 'new';
$node->save();
@ -65,7 +65,7 @@ class BookUninstallTest extends KernelTestBase {
$validation_reasons = \Drupal::service('module_installer')->validateUninstall(['book']);
$this->assertEqual(['To uninstall Book, delete all content that is part of a book'], $validation_reasons['book']);
$book_node = Node::create(array('type' => 'book'));
$book_node = Node::create(array('title' => $this->randomString(), 'type' => 'book'));
$book_node->book['bid'] = FALSE;
$book_node->save();
@ -84,7 +84,7 @@ class BookUninstallTest extends KernelTestBase {
$module_data = _system_rebuild_module_data();
$this->assertFalse(isset($module_data['book']->info['required']), 'The book module is not required.');
$node = Node::create(array('type' => $content_type->id()));
$node = Node::create(array('title' => $this->randomString(), 'type' => $content_type->id()));
$node->save();
// One node exists but is not part of a book therefore the book module is
// not required.

View File

@ -44,6 +44,7 @@ class MigrateCommentTest extends MigrateDrupal6TestBase {
$node = entity_create('node', array(
'type' => 'story',
'nid' => 1,
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();

View File

@ -50,6 +50,7 @@ class CommentUserNameTest extends ViewKernelTestBase {
$storage
->create(array(
'uid' => 0,
'name' => '',
'status' => 0,
))
->save();
@ -75,6 +76,7 @@ class CommentUserNameTest extends ViewKernelTestBase {
$comment = Comment::create([
'subject' => 'My comment title',
'uid' => $this->adminUser->id(),
'name' => $this->adminUser->label(),
'entity_type' => 'entity_test',
'comment_type' => 'entity_test',
'status' => 1,

View File

@ -61,7 +61,7 @@ class ContentTranslationMetadataFieldsTest extends ContentTranslationTestBase {
// Create a new test entity with original values in the default language.
$default_langcode = $this->langcodes[0];
$entity_id = $this->createEntity([], $default_langcode);
$entity_id = $this->createEntity(['title' => $this->randomString()], $default_langcode);
$storage = $entity_manager->getStorage($this->entityTypeId);
$storage->resetCache();
$entity = $storage->load($entity_id);
@ -118,7 +118,7 @@ class ContentTranslationMetadataFieldsTest extends ContentTranslationTestBase {
// Create a new test entity with original values in the default language.
$default_langcode = $this->langcodes[0];
$entity_id = $this->createEntity(['status' => FALSE], $default_langcode);
$entity_id = $this->createEntity(['title' => $this->randomString(), 'status' => FALSE], $default_langcode);
$storage = $entity_manager->getStorage($this->entityTypeId);
$storage->resetCache();
$entity = $storage->load($entity_id);

View File

@ -134,7 +134,7 @@ class ContextualDynamicContextTest extends WebTestBase {
$this->assertResponse(403);
// Verify that link language is properly handled.
$node3->addTranslation('it')->save();
$node3->addTranslation('it')->set('title', $this->randomString())->save();
$id = 'node:node=' . $node3->id() . ':changed=' . $node3->getChangedTime() . '&langcode=it';
$this->drupalGet('node', ['language' => ConfigurableLanguage::createFromLangcode('it')]);
$this->assertContextualLinkPlaceHolder($id);

View File

@ -84,6 +84,7 @@ abstract class MigrateUploadBase extends MigrateDrupal6TestBase {
'type' => 'story',
'nid' => $i,
'vid' => array_shift($vids),
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();

View File

@ -198,7 +198,8 @@ class EntityTest extends NormalizerTestBase {
$original_values = $comment->toArray();
// cid will not exist and hostname will always be denied view access.
unset($original_values['cid'], $original_values['hostname']);
// No value will exist for name as this is only for anonymous users.
unset($original_values['cid'], $original_values['hostname'], $original_values['name']);
$normalized = $this->serializer->normalize($comment, $this->format, ['account' => $account]);

View File

@ -69,7 +69,7 @@ class MigrateMenuLinkTest extends MigrateDrupal6TestBase {
$menu_link = entity_load('menu_link_content', 140);
$this->assertIdentical('Drupal.org', $menu_link->getTitle());
$this->assertIdentical('secondary-links', $menu_link->getMenuName());
$this->assertIdentical('', $menu_link->getDescription());
$this->assertIdentical(NULL, $menu_link->getDescription());
$this->assertIdentical(TRUE, $menu_link->isEnabled());
$this->assertIdentical(FALSE, $menu_link->isExpanded());
$this->assertIdentical(['attributes' => ['title' => '']], $menu_link->link->options);
@ -80,7 +80,7 @@ class MigrateMenuLinkTest extends MigrateDrupal6TestBase {
$menu_link = entity_load('menu_link_content', 393);
$this->assertIdentical('Test 3', $menu_link->getTitle());
$this->assertIdentical('secondary-links', $menu_link->getMenuName());
$this->assertIdentical('', $menu_link->getDescription());
$this->assertIdentical(NULL, $menu_link->getDescription());
$this->assertIdentical(TRUE, $menu_link->isEnabled());
$this->assertIdentical(FALSE, $menu_link->isExpanded());
$this->assertIdentical([], $menu_link->link->options);

View File

@ -33,6 +33,35 @@ class MigrateCckFieldValuesTest extends MigrateNodeTestBase {
$this->installEntitySchema('file');
$node = entity_create('node', array(
'type' => 'story',
'nid' => 2,
'vid' => 12,
'revision_log' => '',
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();
$planet_nodes = [
4 => 6,
5 => 8,
6 => 9,
7 => 10,
8 => 11,
];
foreach ($planet_nodes as $nid => $vid) {
$node = entity_create('node', array(
'type' => 'test_planet',
'nid' => $nid,
'vid' => $vid,
'revision_log' => '',
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();
}
entity_create('field_storage_config', array(
'entity_type' => 'node',
'field_name' => 'field_test',

View File

@ -387,7 +387,6 @@ class Node extends ContentEntityBase implements NodeInterface {
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('view', array(
'label' => 'hidden',

View File

@ -55,7 +55,7 @@ class MigrateNodeTest extends MigrateNodeTestBase {
$this->assertIdentical('Test title', $node_revision->getTitle());
$this->assertIdentical('1', $node_revision->getRevisionAuthor()->id(), 'Node revision has the correct user');
// This is empty on the first revision.
$this->assertIdentical('', $node_revision->revision_log->value);
$this->assertIdentical(NULL, $node_revision->revision_log->value);
// It is pointless to run the second half from MigrateDrupal6Test.
if (empty($this->standalone)) {

View File

@ -82,6 +82,7 @@ abstract class MigrateNodeTestBase extends MigrateDrupal6TestBase {
'nid' => 1,
'vid' => 1,
'revision_log' => '',
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();
@ -91,6 +92,7 @@ abstract class MigrateNodeTestBase extends MigrateDrupal6TestBase {
'nid' => 3,
'vid' => 4,
'revision_log' => '',
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();

View File

@ -111,6 +111,7 @@ class NodeAccessLanguageAwareCombinationTest extends NodeTestBase {
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
@ -121,6 +122,7 @@ class NodeAccessLanguageAwareCombinationTest extends NodeTestBase {
'private' => TRUE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
@ -131,6 +133,7 @@ class NodeAccessLanguageAwareCombinationTest extends NodeTestBase {
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
@ -141,6 +144,7 @@ class NodeAccessLanguageAwareCombinationTest extends NodeTestBase {
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
@ -151,6 +155,7 @@ class NodeAccessLanguageAwareCombinationTest extends NodeTestBase {
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
@ -161,6 +166,7 @@ class NodeAccessLanguageAwareCombinationTest extends NodeTestBase {
'private' => TRUE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();

View File

@ -104,6 +104,7 @@ class NodeAccessLanguageAwareTest extends NodeTestBase {
'field_private' => array(array('value' => 0)),
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
@ -113,6 +114,7 @@ class NodeAccessLanguageAwareTest extends NodeTestBase {
'field_private' => array(array('value' => 0)),
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
@ -122,6 +124,7 @@ class NodeAccessLanguageAwareTest extends NodeTestBase {
'field_private' => array(array('value' => 1)),
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
@ -131,6 +134,7 @@ class NodeAccessLanguageAwareTest extends NodeTestBase {
'field_private' => array(array('value' => 1)),
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();

View File

@ -55,6 +55,11 @@ class NodeValidationTest extends EntityUnitTestBase {
$this->assertEqual($violations[0]->getPropertyPath(), 'title');
$this->assertEqual($violations[0]->getMessage(), 'This value should not be null.');
$node->set('title', '');
$violations = $node->validate();
$this->assertEqual(count($violations), 1, 'Violation found when title is set to an empty string.');
$this->assertEqual($violations[0]->getPropertyPath(), 'title');
// Make the title valid again.
$node->set('title', $this->randomString());
// Save the node so that it gets an ID and a changed date.

View File

@ -26,7 +26,7 @@ class NodeRevisionWizardTest extends WizardTestBase {
// Create two nodes with two revision.
$node_storage = \Drupal::entityManager()->getStorage('node');
/** @var \Drupal\node\NodeInterface $node */
$node = $node_storage->create(array('type' => 'article', 'created' => REQUEST_TIME + 40));
$node = $node_storage->create(array('title' => $this->randomString(), 'type' => 'article', 'created' => REQUEST_TIME + 40));
$node->save();
$node = $node->createDuplicate();
@ -34,7 +34,7 @@ class NodeRevisionWizardTest extends WizardTestBase {
$node->created->value = REQUEST_TIME + 20;
$node->save();
$node = $node_storage->create(array('type' => 'article', 'created' => REQUEST_TIME + 30));
$node = $node_storage->create(array('title' => $this->randomString(), 'type' => 'article', 'created' => REQUEST_TIME + 30));
$node->save();
$node = $node->createDuplicate();

View File

@ -56,6 +56,7 @@ abstract class OptionsTestBase extends ViewKernelTestBase {
$settings = [];
$settings['type'] = 'article';
$settings['title'] = $this->randomString();
$settings['field_test_list_string'][]['value'] = $this->fieldValues[0];
$settings['field_test_list_integer'][]['value'] = 0;

View File

@ -132,7 +132,6 @@ class Shortcut extends ContentEntityBase implements ShortcutInterface {
->setDescription(t('The name of the shortcut.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('form', array(
'type' => 'string_textfield',

View File

@ -677,6 +677,7 @@ class EntityFieldTest extends EntityUnitTestBase {
$node = entity_create('node', array(
'type' => 'page',
'uid' => $user->id(),
'title' => $this->randomString(),
));
$reference->setValue($node);
$violations = $reference->validate();
@ -699,6 +700,7 @@ class EntityFieldTest extends EntityUnitTestBase {
$node = entity_create('node', array(
'type' => 'article',
'uid' => $user->id(),
'title' => $this->randomString(),
));
$node->save();
$reference->setValue($node);

View File

@ -106,6 +106,7 @@ class EntityTranslationFormTest extends WebTestBase {
// Create a body translation and check the form language.
$langcode2 = $this->langcodes[1];
$node->getTranslation($langcode2)->title->value = $this->randomString();
$node->getTranslation($langcode2)->body->value = $this->randomMachineName(16);
$node->getTranslation($langcode2)->setOwnerId($web_user->id());
$node->save();

View File

@ -51,7 +51,7 @@ class UninstallTest extends WebTestBase {
$node_type->setThirdPartySetting('module_test', 'key', 'value');
$node_type->save();
// Add a node to prevent node from being uninstalled.
$node = entity_create('node', array('type' => 'uninstall_blocker'));
$node = entity_create('node', array('type' => 'uninstall_blocker', 'title' => $this->randomString()));
$node->save();
$this->drupalGet('admin/modules/uninstall');

View File

@ -71,6 +71,7 @@ abstract class MigrateTermNodeTestBase extends MigrateDrupal6TestBase {
'type' => 'story',
'nid' => $i,
'vid' => array_shift($vids),
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();

View File

@ -463,13 +463,14 @@ class User extends ContentEntityBase implements UserInterface {
$fields['name'] = BaseFieldDefinition::create('string')
->setLabel(t('Name'))
->setDescription(t('The name of this user.'))
->setDefaultValue('')
->setRequired(TRUE)
->setConstraints(array(
// No Length constraint here because the UserName constraint also covers
// that.
'UserName' => array(),
'UserNameUnique' => array(),
));
$fields['name']->getItemDefinition()->setClass('\Drupal\user\UserNameItem');
$fields['pass'] = BaseFieldDefinition::create('password')
->setLabel(t('Password'))

View File

@ -88,6 +88,7 @@ class UserRoleConditionTest extends KernelTestBase {
// Setup an anonymous user for our tests.
$this->anonymous = User::create(array(
'name' => '',
'uid' => 0,
));
$this->anonymous->save();

View File

@ -42,8 +42,9 @@ class UserRoleDeleteTest extends KernelTestBase {
// Create user and assign both test roles.
$values = array(
'uid' => 1,
'roles' => array('test_role_one', 'test_role_two'),
'uid' => 1,
'name' => $this->randomString(),
'roles' => array('test_role_one', 'test_role_two'),
);
$user = User::create($values);
$user->save();

View File

@ -41,6 +41,8 @@ class HandlerFieldUserNameTest extends UserTestBase {
$this->executeView($view);
$anon_name = $this->config('user.settings')->get('anonymous');
$view->result[0]->_entity->setUsername('');
$view->result[0]->_entity->uid->value = 0;
$render = $renderer->executeInRenderContext(new RenderContext(), function () use ($view) {
return $view->field['name']->advancedRender($view->result[0]);
});

View File

@ -74,7 +74,7 @@ abstract class UserKernelTestBase extends ViewKernelTestBase {
user_role_grant_permissions('multiple_permissions', array('administer permissions', 'administer users', 'access user profiles'));
// Setup a user without an extra role.
$this->users[] = $account = $this->userStorage->create(array());
$this->users[] = $account = $this->userStorage->create(['name' => $this->randomString()]);
$account->save();
// Setup a user with just the first role (so no permission beside the
// ones from the authenticated role).

View File

@ -0,0 +1,31 @@
<?php
/**
* @file
* Contains \Drupal\user\UserNameItem.
*/
namespace Drupal\user;
use Drupal\Core\Field\Plugin\Field\FieldType\StringItem;
/**
* Defines a custom field item class for the 'name' user entity field.
*/
class UserNameItem extends StringItem {
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')->getValue();
// Take into account that the name of the anonymous user is an empty string.
if ($this->getEntity()->isAnonymous()) {
return $value === NULL;
}
return $value === NULL || $value === '';
}
}

View File

@ -70,6 +70,7 @@ function user_install() {
->create(array(
'uid' => 0,
'status' => 0,
'name' => '',
))
->save();

View File

@ -57,7 +57,7 @@ class FieldEntityTest extends ViewTestBase {
$account = entity_create('user', array('name' => $this->randomMachineName(), 'bundle' => 'user'));
$account->save();
$node = entity_create('node', array('uid' => $account->id(), 'type' => 'page'));
$node = entity_create('node', array('uid' => $account->id(), 'type' => 'page', 'title' => $this->randomString()));
$node->save();
$comment = entity_create('comment', array(
'uid' => $account->id(),

View File

@ -60,7 +60,7 @@ class FilterEntityBundleTest extends ViewTestBase {
foreach ($this->entityBundles as $key => $info) {
for ($i = 0; $i < 5; $i++) {
$entity = entity_create('node', array('label' => $this->randomMachineName(), 'uid' => 1, 'type' => $key));
$entity = entity_create('node', array('title' => $this->randomString(), 'uid' => 1, 'type' => $key));
$entity->save();
$this->entities[$key][$entity->id()] = $entity;
$this->entities['count']++;

View File

@ -74,7 +74,7 @@ class FieldFieldTest extends ViewKernelTestBase {
$this->installEntitySchema('entity_test_rev');
// Bypass any field access.
$this->adminUser = User::create();
$this->adminUser = User::create(['name' => $this->randomString()]);
$this->adminUser->save();
$this->container->get('current_user')->setAccount($this->adminUser);