diff --git a/tests/multiple_value_widget.test b/tests/multiple_value_widget.test index 689eb07..56ff2eb 100644 --- a/tests/multiple_value_widget.test +++ b/tests/multiple_value_widget.test @@ -1,10 +1,13 @@ 'Core tests', @@ -13,6 +16,9 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { ); } + /** + * {@inheritDoc} + */ public function setUp() { parent::setUp('multiple_value_widget'); $this->drupalLogin($this->getAdminUser()); @@ -68,7 +74,8 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { field_create_field($field_list); $instance = array( - 'field_name' => 'field_list', + 'field_name' => $field_list['field_name'], + 'label' => ucfirst(str_replace('_', ' ', $field_list['field_name'])), 'entity_type' => 'node', 'bundle' => 'page', 'widget' => array( @@ -88,7 +95,8 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { ); field_create_field($field_single_text); $instance = array( - 'field_name' => 'field_single_text', + 'field_name' => $field_single_text['field_name'], + 'label' => ucfirst(str_replace('_', ' ', $field_single_text['field_name'])), 'entity_type' => 'node', 'bundle' => 'page', 'widget' => array( @@ -102,6 +110,34 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { ); field_create_instance($instance); + // Required field with unlimited cardinality. + $field_required_multiple_text = array( + 'field_name' => 'field_required_multiple_text', + 'cardinality' => -1, + 'type' => 'text', + 'settings' => array( + 'max_length' => 50, + ), + ); + field_create_field($field_required_multiple_text); + $instance = array( + 'field_name' => $field_required_multiple_text['field_name'], + 'label' => ucfirst(str_replace('_', ' ', $field_required_multiple_text['field_name'])), + 'entity_type' => 'node', + 'bundle' => 'page', + 'widget' => array( + 'type' => 'text_textfield', + 'settings' => array('multiple_value_widget' => 'tabs'), + ), + 'display' => array( + 'default' => array( + 'type' => 'text_default', + ), + ), + 'required' => TRUE, + ); + field_create_instance($instance); + } /** @@ -149,7 +185,7 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { $this->assertEqual($results, 0); } - // Check that the previous xpath expressions target anything. + // Check that the previous xpath expressions target something. // This is testing CARDINALITY = 1 for a field with FIELD_BEHAVIOR_DEFAULT. // In reality, the fields are hidden for CARDINALITY = 1 via #states. $this->drupalGet('admin/structure/types/manage/page/fields/field_single_text'); @@ -161,7 +197,7 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { } /** - * Test generated HTML markup. + * Tests generated HTML markup. * * @see theme_multiple_value_widget_group() */ @@ -179,7 +215,7 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { $tab_title = (string) reset($results); $this->assertEqual($tab_title, 'Body (1)'); // Check that the tabs specific assets/settings are present. - $this->assertRaw('"mvw":{"mvw-body":"tabs"}'); + $this->assertRaw('"mvw-body":"tabs"'); $this->assertRaw('jquery.ui.tabs'); // Body is set anew to 'blocks'. @@ -193,7 +229,7 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { $results = count($this->xpath($xpath)); $this->assertEqual($results, 1); // Check that the blocks specific settings are present. - $this->assertRaw('"mvw":{"mvw-body":"blocks"}'); + $this->assertRaw('"mvw-body":"blocks"'); // Body is set anew to 'accordion' and cardinality to 2. $edit = array( @@ -207,7 +243,7 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { $results = count($this->xpath($xpath)); $this->assertEqual($results, 2); // Check that the accordion specific assets/settings are present. - $this->assertRaw('"mvw":{"mvw-body":"accordion"}'); + $this->assertRaw('"mvw-body":"accordion"'); $this->assertRaw('jquery.ui.accordion'); // Check that the rest of the assets are added correctly. @@ -217,7 +253,7 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { } /** - * Test API. + * Tests the exposed API. * * @see hook_multiple_value_widget_element_title_alter() */ @@ -233,4 +269,62 @@ class MultipleValueWidgetTestCase extends DrupalWebTestCase { $this->assertEqual($tab_title, MULTIPLE_VALUE_WIDGET_TEST_MODIFIED_TITLE); } + /** + * Tests the rendering of the multiple value widget after a failed validation. + */ + public function testWidgetWeightsAndValidation() { + // Create a page. + $this->drupalGet('node/add/page'); + + // Test that validation fails in the simplest case, with AJAX. + $this->drupalPostAjax(NULL, array(), array('field_required_multiple_text_add_more' => t('Add another item'))); + $this->assertText('field is required.'); + // Test that validation fails in the simplest case, without AJAX. + $this->drupalPost(NULL, array(), t('Save')); + $this->assertText('field is required.'); + + // Test that validation succeeds. + $this->drupalGet('node/add/page'); + $field_required_multiple_text_value_0 = $this->randomName(); + $edit = array( + 'title' => $this->randomName(), + 'field_required_multiple_text[und][0][value]' => $field_required_multiple_text_value_0, + ); + // With Ajax; we also confirm that a field item is correctly added, + // after the form is rebuild. + $this->drupalPostAjax(NULL, $edit, array('field_required_multiple_text_add_more' => t('Add another item'))); + $this->assertFieldByName('field_required_multiple_text[und][1][value]', NULL, t('Validation succeeded since we got a new item added.')); + // Without Ajax. + $this->drupalPost(NULL, $edit, t('Save')); + $this->assertText('Basic page ' . $edit['title'] . ' has been created.'); + + // Get a reference to the new node. + $node = $this->drupalGetNodeByTitle($edit['title']); + $this->drupalGet('node/' . $node->nid . '/edit'); + + // Test rearranging fields. + $field_required_multiple_text_value_1 = $this->randomName(); + $edit = array( + 'field_required_multiple_text[und][0][_weight]' => 1, + 'field_required_multiple_text[und][1][_weight]' => 0, + 'field_required_multiple_text[und][1][value]' => $field_required_multiple_text_value_1, + ); + $this->drupalPost(NULL, $edit, t('Save')); + $node = $this->drupalGetNodeByTitle($node->title, TRUE); + $this->assertEqual($node->field_required_multiple_text[LANGUAGE_NONE][0]['value'], $field_required_multiple_text_value_1); + $this->assertEqual($node->field_required_multiple_text[LANGUAGE_NONE][1]['value'], $field_required_multiple_text_value_0); + + // Test validation fails after rearranging fields. + $this->drupalGet('node/' . $node->nid . '/edit'); + $edit = array( + 'field_required_multiple_text[und][0][value]' => '', + ); + // With Ajax. + $this->drupalPostAjax(NULL, $edit, array('field_required_multiple_text_add_more' => t('Add another item'))); + $this->assertText('field is required.'); + // Without Ajax. + $this->drupalPost(NULL, $edit, t('Save')); + $this->assertText('field is required.'); + } + }