Commit 764890d6 authored by effulgentsia's avatar effulgentsia

Issue #2349819 by amateescu, swentel, Leksat, larowlan, dawehner, Gábor...

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
parent 8dc9763d
......@@ -63,4 +63,16 @@ public function preSave() {
}
}
}
/**
* {@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;
}
}
......@@ -40,4 +40,12 @@ public static function propertyDefinitions(FieldStorageDefinitionInterface $fiel
return $properties;
}
/**
* {@inheritdoc}
*/
public function isEmpty() {
$value = $this->get('value')->getValue();
return $value === NULL || $value === '';
}
}
......@@ -47,9 +47,10 @@ public function testProcess() {
*/
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));
}
......
......@@ -57,7 +57,7 @@ public function testBookUninstall() {
$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 @@ public function testBookUninstall() {
$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 @@ public function testBookUninstall() {
$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.
......
......@@ -44,6 +44,7 @@ protected function setUp() {
$node = entity_create('node', array(
'type' => 'story',
'nid' => 1,
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();
......
......@@ -50,6 +50,7 @@ protected function setUp($import_test_views = TRUE) {
$storage
->create(array(
'uid' => 0,
'name' => '',
'status' => 0,
))
->save();
......@@ -75,6 +76,7 @@ protected function setUp($import_test_views = TRUE) {
$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,
......
......@@ -61,7 +61,7 @@ public function testSkipUntranslatable() {
// 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 @@ public function testSetTranslatable() {
// 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);
......
......@@ -134,7 +134,7 @@ function testDifferentPermissions() {
$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);
......
......@@ -84,6 +84,7 @@ protected function setUp() {
'type' => 'story',
'nid' => $i,
'vid' => array_shift($vids),
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();
......
......@@ -198,7 +198,8 @@ public function testComment() {
$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]);
......
......@@ -69,7 +69,7 @@ public function testMenuLinks() {
$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 @@ public function testMenuLinks() {
$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);
......
......@@ -33,6 +33,35 @@ protected function setUp() {
$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',
......
......@@ -387,7 +387,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setRequired(TRUE)
->setTranslatable(TRUE)
->setRevisionable(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('view', array(
'label' => 'hidden',
......
......@@ -55,7 +55,7 @@ public function testNode() {
$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)) {
......
......@@ -82,6 +82,7 @@ protected function setUp() {
'nid' => 1,
'vid' => 1,
'revision_log' => '',
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();
......@@ -91,6 +92,7 @@ protected function setUp() {
'nid' => 3,
'vid' => 4,
'revision_log' => '',
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();
......
......@@ -111,6 +111,7 @@ protected function setUp() {
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
......@@ -121,6 +122,7 @@ protected function setUp() {
'private' => TRUE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
......@@ -131,6 +133,7 @@ protected function setUp() {
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 0;
$node->save();
......@@ -141,6 +144,7 @@ protected function setUp() {
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
......@@ -151,6 +155,7 @@ protected function setUp() {
'private' => FALSE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
......@@ -161,6 +166,7 @@ protected function setUp() {
'private' => TRUE,
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
......
......@@ -104,6 +104,7 @@ protected function setUp() {
'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 @@ protected function setUp() {
'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 @@ protected function setUp() {
'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 @@ protected function setUp() {
'field_private' => array(array('value' => 1)),
));
$translation = $node->getTranslation('ca');
$translation->title->value = $this->randomString();
$translation->field_private->value = 1;
$node->save();
......
......@@ -55,6 +55,11 @@ public function testValidation() {
$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.
......
......@@ -26,7 +26,7 @@ public function testViewAdd() {
// 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 @@ public function testViewAdd() {
$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();
......
......@@ -56,6 +56,7 @@ protected function setUp() {
$settings = [];
$settings['type'] = 'article';
$settings['title'] = $this->randomString();
$settings['field_test_list_string'][]['value'] = $this->fieldValues[0];
$settings['field_test_list_integer'][]['value'] = 0;
......
......@@ -132,7 +132,6 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDescription(t('The name of the shortcut.'))
->setRequired(TRUE)
->setTranslatable(TRUE)
->setDefaultValue('')
->setSetting('max_length', 255)
->setDisplayOptions('form', array(
'type' => 'string_textfield',
......
......@@ -677,6 +677,7 @@ public function testEntityConstraintValidation() {
$node = entity_create('node', array(
'type' => 'page',
'uid' => $user->id(),
'title' => $this->randomString(),
));
$reference->setValue($node);
$violations = $reference->validate();
......@@ -699,6 +700,7 @@ public function testEntityConstraintValidation() {
$node = entity_create('node', array(
'type' => 'article',
'uid' => $user->id(),
'title' => $this->randomString(),
));
$node->save();
$reference->setValue($node);
......
......@@ -106,6 +106,7 @@ function testEntityFormLanguage() {
// 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();
......
......@@ -51,7 +51,7 @@ function testUninstallPage() {
$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');
......
......@@ -71,6 +71,7 @@ protected function setUp() {
'type' => 'story',
'nid' => $i,
'vid' => array_shift($vids),
'title' => $this->randomString(),
));
$node->enforceIsNew();
$node->save();
......
......@@ -463,13 +463,14 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
$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'))
......
......@@ -88,6 +88,7 @@ protected function setUp() {
// Setup an anonymous user for our tests.
$this->anonymous = User::create(array(
'name' => '',
'uid' => 0,
));
$this->anonymous->save();
......
......@@ -42,8 +42,9 @@ public function testRoleDeleteUserRoleReferenceDelete() {
// 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();
......
......@@ -41,6 +41,8 @@ public function testUserName() {
$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]);
});
......
......@@ -74,7 +74,7 @@ protected function setupPermissionTestData() {
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).
......
<?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 === '';
}
}
......@@ -70,6 +70,7 @@ function user_install() {
->create(array(
'uid' => 0,
'status' => 0,
'name' => '',
))
->save();
......
......@@ -57,7 +57,7 @@ public function testGetEntity() {
$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(),
......
......@@ -60,7 +60,7 @@ protected function setUp() {
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']++;
......
......@@ -74,7 +74,7 @@ protected function setUp() {
$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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment