diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentApprovalTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentApprovalTest.php index 62a7812..003ce32 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentApprovalTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentApprovalTest.php @@ -47,7 +47,7 @@ function testApprovalAdminInterface() { // Get unapproved comment id. $this->drupalLogin($this->admin_user); $anonymous_comment4 = $this->getUnapprovedComment($subject); - $anonymous_comment4 = entity_create('comment', array('id' => $anonymous_comment4, 'subject' => $subject, 'comment' => $body)); + $anonymous_comment4 = entity_create('comment', array('id' => $anonymous_comment4, 'node_type' => '', 'subject' => $subject, 'comment' => $body)); $this->drupalLogout(); $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.'); @@ -111,7 +111,7 @@ function testApprovalNodeInterface() { // Get unapproved comment id. $this->drupalLogin($this->admin_user); $anonymous_comment4 = $this->getUnapprovedComment($subject); - $anonymous_comment4 = entity_create('comment', array('id' => $anonymous_comment4, 'subject' => $subject, 'comment' => $body)); + $anonymous_comment4 = entity_create('comment', array('id' => $anonymous_comment4, 'node_type' => '', 'subject' => $subject, 'comment' => $body)); $this->drupalLogout(); $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.'); diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentCSSTest.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentCSSTest.php index 5cf0720..1294de4 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentCSSTest.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentCSSTest.php @@ -50,6 +50,7 @@ function testCommentClasses() { // Add a comment. $comment = entity_create('comment', array( 'nid' => $node->nid, + 'node_type' => 'node_type_' . $node->bundle(), 'uid' => $case['comment_uid'], 'status' => $case['comment_status'], 'subject' => $this->randomName(), diff --git a/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php b/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php index 902dbdd..18c2724 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/CommentTestBase.php @@ -145,7 +145,13 @@ function postComment($node, $comment, $subject = '', $contact = NULL) { } if (isset($match[1])) { - return entity_create('comment', array('id' => $match[1], 'subject' => $subject, 'comment' => $comment)); + $values = array( + 'id' => $match[1], + 'node_type' => is_object($node) ? 'node_comment_' . $node->bundle() : '', + 'subject' => $subject, + 'comment' => $comment, + ); + return entity_create('comment', $values); } } diff --git a/core/modules/comment/lib/Drupal/comment/Tests/Views/CommentTestBase.php b/core/modules/comment/lib/Drupal/comment/Tests/Views/CommentTestBase.php index 476c0ef..c913883 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/Views/CommentTestBase.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/Views/CommentTestBase.php @@ -48,6 +48,7 @@ function setUp() { 'nid' => $this->node_user_commented->nid, 'cid' => '', 'pid' => '', + 'node_type' => '', ); $this->comment = entity_create('comment', $comment); $this->comment->save(); diff --git a/core/modules/comment/lib/Drupal/comment/Tests/Views/DefaultViewRecentComments.php b/core/modules/comment/lib/Drupal/comment/Tests/Views/DefaultViewRecentComments.php index b8e7d75..60a3365 100644 --- a/core/modules/comment/lib/Drupal/comment/Tests/Views/DefaultViewRecentComments.php +++ b/core/modules/comment/lib/Drupal/comment/Tests/Views/DefaultViewRecentComments.php @@ -77,11 +77,10 @@ public function setUp() { // Create some comments and attach them to the created node. for ($i = 0; $i < $this->masterDisplayResults; $i++) { - $comment = entity_create('comment', array()); + $comment = entity_create('comment', array('node_type' => 'comment_node_' . $this->node->type)); $comment->uid = 0; $comment->nid = $this->node->nid; $comment->subject = 'Test comment ' . $i; - $comment->node_type = 'comment_node_' . $this->node->type; $comment->comment_body[LANGUAGE_NOT_SPECIFIED][0]['value'] = 'Test body ' . $i; $comment->comment_body[LANGUAGE_NOT_SPECIFIED][0]['format'] = 'full_html'; --- a/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FieldAttachStorageTest.php @@ -340,9 +340,12 @@ function testFieldAttachSaveMissingDataDefaultValue() { $this->instance['default_value_function'] = 'field_test_default_value'; field_update_instance($this->instance); + // Verify that fields are populated with default values. $entity_type = 'test_entity'; $entity_init = field_test_create_entity(); $langcode = LANGUAGE_NOT_SPECIFIED; + $default = field_test_default_value($entity_type, $entity_init, $this->field, $this->instance); + $this->assertEqual($entity_init->{$this->field_name}[$langcode], $default, 'Default field value correctly populated.'); // Insert: Field is NULL. $entity = clone($entity_init); @@ -350,6 +353,7 @@ function testFieldAttachSaveMissingDataDefaultValue() { field_attach_insert($entity_type, $entity); $entity = clone($entity_init); + $entity->{$this->field_name} = array(); field_attach_load($entity_type, array($entity->ftid => $entity)); $this->assertTrue(empty($entity->{$this->field_name}[$langcode]), 'Insert: NULL field results in no value saved'); @@ -359,9 +363,14 @@ function testFieldAttachSaveMissingDataDefaultValue() { field_attach_insert($entity_type, $entity); $entity = clone($entity_init); + $entity->{$this->field_name} = array(); field_attach_load($entity_type, array($entity->ftid => $entity)); - $values = field_test_default_value($entity_type, $entity, $this->field, $this->instance); - $this->assertEqual($entity->{$this->field_name}[$langcode], $values, 'Insert: missing field results in default value saved'); + $this->assertEqual($entity->{$this->field_name}[$langcode], $default, 'Insert: missing field results in default value saved'); + + // Verify that prepopulated field values are not overwritten by defaults. + $value = array(array('value' => $default[0]['value'] - mt_rand(1, 127))); + $entity = entity_create('test_entity', array('fttype' => $entity_init->bundle(), $this->field_name => array($langcode => $value))); + $this->assertEqual($entity->{$this->field_name}[$langcode], $value, 'Prepopulated field value correctly maintained.'); } /** diff --git a/core/modules/field/lib/Drupal/field/Tests/FormTest.php b/core/modules/field/lib/Drupal/field/Tests/FormTest.php index b11638f..65fd25b 100644 --- a/core/modules/field/lib/Drupal/field/Tests/FormTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/FormTest.php @@ -69,7 +69,6 @@ function testFieldFormSingle() { $this->assertText($token_description, 'Token replacement for description is displayed'); $this->assertFieldByName("{$this->field_name}[$langcode][0][value]", '', 'Widget is displayed'); $this->assertNoField("{$this->field_name}[$langcode][1][value]", 'No extraneous widget is displayed'); - // TODO : check that the widget is populated with default value ? // Check that hook_field_widget_form_alter() does not believe this is the // default value form. @@ -113,7 +112,34 @@ function testFieldFormSingle() { entity_get_controller('test_entity')->resetCache(array($id)); $entity = field_test_entity_test_load($id); $this->assertIdentical($entity->{$this->field_name}, array(), 'Field was emptied'); + } + /** + * Tests field widget default values on entity forms. + */ + function testFieldFormDefaultValue() { + $this->field = $this->field_single; + $this->field_name = $this->field['field_name']; + $this->instance['field_name'] = $this->field_name; + $default = rand(1, 127); + $this->instance['default_value'] = array(array('value' => $default)); + field_create_field($this->field); + field_create_instance($this->instance); + $langcode = LANGUAGE_NOT_SPECIFIED; + + // Display creation form. + $this->drupalGet('test-entity/add/test_bundle'); + // Test that the default value is displayed correctly. + $this->assertFieldByXpath("//input[@name='{$this->field_name}[$langcode][0][value]' and @value='$default']"); + + // Try to submit an empty value. + $edit = array("{$this->field_name}[$langcode][0][value]" => ''); + $this->drupalPost(NULL, $edit, t('Save')); + preg_match('|test-entity/manage/(\d+)/edit|', $this->url, $match); + $id = $match[1]; + $this->assertRaw(t('test_entity @id has been created.', array('@id' => $id)), 'Entity was created.'); + $entity = field_test_entity_test_load($id); + $this->assertTrue(empty($entity->{$this->field_name}), 'Field is now empty.'); } function testFieldFormSingleRequired() { diff --git a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php index 8f813c0..1c324f5 100644 --- a/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php +++ b/core/modules/field/lib/Drupal/field/Tests/TranslationTest.php @@ -242,6 +242,52 @@ function testTranslatableFieldSaveLoad() { } $this->assertTrue($result, format_string('%language translation correctly handled.', array('%language' => $langcode))); } + + // Test default values. + $field_name_default = drupal_strtolower($this->randomName() . '_field_name'); + $field = $this->field; + $field['field_name'] = $field_name_default; + $instance = $this->instance; + $instance['field_name'] = $field_name_default; + $default = rand(1, 127); + $instance['default_value'] = array(array('value' => $default)); + field_create_field($field); + field_create_instance($instance); + $translation_langcodes = array_slice($available_langcodes, 0, 2); + asort($translation_langcodes); + $translation_langcodes = array_values($translation_langcodes); + + $eid++; + $evid++; + $values = array('eid' => $eid, 'evid' => $evid, 'fttype' => $instance['bundle'], 'langcode' => $translation_langcodes[0]); + foreach ($translation_langcodes as $langcode) { + $values[$this->field_name][$langcode] = $this->_generateTestFieldValues($this->field['cardinality']); + } + $entity = entity_create($entity_type, $values); + + ksort($entity->{$field_name_default}); + $field_langcodes = array_keys($entity->{$field_name_default}); + $this->assertEqual($translation_langcodes, $field_langcodes, 'Missing translations did not get a default value.'); + + foreach ($entity->{$field_name_default} as $langcode => $items) { + $this->assertEqual($items, $instance['default_value'], format_string('Default value correctly populated for language %language.', array('%language' => $langcode))); + } + + // Check that explicit empty values are not overridden with default values. + foreach (array(NULL, array()) as $empty_items) { + $eid++; + $evid++; + $values = array('eid' => $eid, 'evid' => $evid, 'fttype' => $instance['bundle'], 'langcode' => $translation_langcodes[0]); + foreach ($translation_langcodes as $langcode) { + $values[$this->field_name][$langcode] = $this->_generateTestFieldValues($this->field['cardinality']); + $values[$field_name_default][$langcode] = $empty_items; + } + $entity = entity_create($entity_type, $values); + + foreach ($entity->{$field_name_default} as $langcode => $items) { + $this->assertEqual($items, $empty_items, format_string('Empty value correctly populated for language %language.', array('%language' => $langcode))); + } + } } /** diff --git a/core/modules/field/tests/modules/field_test/field_test.entity.inc b/core/modules/field/tests/modules/field_test/field_test.entity.inc index 517f5cb..bcfd7a0 100644 --- a/core/modules/field/tests/modules/field_test/field_test.entity.inc +++ b/core/modules/field/tests/modules/field_test/field_test.entity.inc @@ -121,7 +121,7 @@ function field_test_delete_bundle($bundle) { * Creates a basic test_entity entity. */ function field_test_create_entity($id = 1, $vid = 1, $bundle = 'test_bundle', $label = '') { - $entity = entity_create('test_entity', array()); + $entity = entity_create('test_entity', array('fttype' => $bundle)); // Only set id and vid properties if they don't come as NULL (creation form). if (isset($id)) { $entity->ftid = $id; @@ -131,7 +131,6 @@ function field_test_create_entity($id = 1, $vid = 1, $bundle = 'test_bundle', $l // Flag to make sure that the provided vid is used for a new revision. $entity->use_provided_revision_id = $vid; } - $entity->fttype = $bundle; $label = !empty($label) ? $label : $bundle . ' label'; $entity->ftlabel = $label; diff --git a/core/modules/forum/lib/Drupal/forum/Tests/ForumBlockTest.php b/core/modules/forum/lib/Drupal/forum/Tests/ForumBlockTest.php index e121cb8..0c01032 100644 --- a/core/modules/forum/lib/Drupal/forum/Tests/ForumBlockTest.php +++ b/core/modules/forum/lib/Drupal/forum/Tests/ForumBlockTest.php @@ -110,6 +110,7 @@ function testActiveForumTopicsBlock() { $node = $this->drupalGetNodeByTitle($topics[$index]); $comment = entity_create('comment', array( 'nid' => $node->nid, + 'node_type' => 'node_type_' . $node->bundle(), 'subject' => $this->randomString(20), 'comment_body' => array(LANGUAGE_NOT_SPECIFIED => $this->randomString(256)), 'created' => $timestamp + $index, diff --git a/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php b/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php index 0667e1b..2ca53b1 100644 --- a/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php +++ b/core/modules/node/lib/Drupal/node/Tests/NodeAccessPagerTest.php @@ -47,6 +47,7 @@ public function testCommentPager() { for ($i = 0; $i < 60; $i++) { $comment = entity_create('comment', array( 'nid' => $node->nid, + 'node_type' => 'node_type_' . $node->bundle(), 'subject' => $this->randomName(), 'comment_body' => array( LANGUAGE_NOT_SPECIFIED => array( diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php index 91c292c..4cad51a 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php @@ -85,6 +85,7 @@ public function testCommentHooks() { $nid = $node->nid; $comment = entity_create('comment', array( + 'node_type' => 'node_type_' . $node->bundle(), 'cid' => NULL, 'pid' => 0, 'nid' => $nid, diff --git a/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php b/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php index 1e5b72f..01eaca3 100644 --- a/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php +++ b/core/modules/views/lib/Drupal/views/Tests/DefaultViewsTest.php @@ -108,6 +108,7 @@ protected function setUp() { $comment = array( 'uid' => $user->uid, 'nid' => $node->nid, + 'node_type' => 'node_type_' . $node->bundle(), ); entity_create('comment', $comment)->save(); }