diff --git a/tests/submission.file.test b/tests/submission.file.test index 6d2fd78..9e988a6 100644 --- a/tests/submission.file.test +++ b/tests/submission.file.test @@ -21,34 +21,34 @@ public static function getInfo() { * Test that a form can have a file component added and have it show for * authenticated visitors. */ - // function testAuthBasicSubmission() { - // // Create the node with a file component. - // $node = $this->createTestWebform(); - // - // // Run the basic submission tests against this webform. - // $this->basicSubmissionTest($node->nid); - // - // // Confirm the submission was successful. - // $this->assertSubmission($node->nid); - // } + function testAuthBasicSubmission() { + // Create the node with a file component. + $node = $this->createTestWebform(); + + // Run the basic submission tests against this webform. + $this->basicSubmissionTest($node->nid); + + // Confirm the submission was successful. + $this->assertSubmission($node->nid); + } /** * Test that a form can have a file component added and have it show for * anonymous visitors. */ - // function testAnonBasicSubmission() { - // // Create the node with a file component. - // $node = $this->createTestWebform(); - // - // // Logout. - // $this->drupalLogout(); - // - // // Run the basic submission tests against this webform. - // $this->basicSubmissionTest($node->nid); - // - // // Confirm the submission was successful. - // $this->assertSubmission($node->nid); - // } + function testAnonBasicSubmission() { + // Create the node with a file component. + $node = $this->createTestWebform(); + + // Logout. + $this->drupalLogout(); + + // Run the basic submission tests against this webform. + $this->basicSubmissionTest($node->nid); + + // Confirm the submission was successful. + $this->assertSubmission($node->nid); + } /** * Confirm a file can be submitted by an authenticated user. @@ -86,45 +86,36 @@ function testAnonFileSubmission() { /** * Confirm that a file can be submitted when another component is required. */ - // function testAuthFileRequiredSubmission() { - // // Create the node with a file component. - // $node = $this->createTestWebform(); - // - // // Submit the webform with a file but also a missing required text field, to - // // confirm that the file can still be submitted. - // $edit = array(); - // $this->drupalPost('node/' . $node->nid, $edit, 'Submit', array(), array(), 'webform-client-form-' . $node->nid); - // foreach ($node->webform['components'] as $component) { - // if ($component['type'] == 'textcomponent') { - // $this->assertText(t('!name component is required.', array('!name' => $component['name']))); - // } - // } - // $this->assertResponse(200); - // $this->assertText(t('Thanks!')); - // } + function testAuthFileRequiredSubmission() { + // Create the node with a file component. + $node = $this->createTestWebform(); + + // Run the submission tests + $this->fileRequiredSubmissionTest($node->nid); + + // Confirm the submission was successful. + $this->assertSubmission($node->nid); + $this->assertFileSubmission($node->nid); + } /** - * Confirm that a file can be submitted by anonymous visitors when another component is required. + * Confirm that a file can be submitted by anonymous visitors when another + * component is required. */ - // function testAnonFileRequiredSubmission() { - // // Create the node with a file component. - // $node = $this->createTestWebform(); - // - // // Logout. - // $this->drupalLogout(); - // - // // Submit the webform with a file but also a missing required text field, to - // // confirm that the file can still be submitted. - // $edit = array(); - // $this->drupalPost('node/' . $node->nid, $edit, 'Submit', array(), array(), 'webform-client-form-' . $node->nid); - // foreach ($node->webform['components'] as $component) { - // if ($component['type'] == 'textcomponent') { - // $this->assertText(t('!name component is required.', array('!name' => $component['name']))); - // } - // } - // $this->assertResponse(200); - // $this->assertText(t('Thanks!')); - // } + function testAnonFileRequiredSubmission() { + // Create the node with a file component. + $node = $this->createTestWebform(); + + // Logout. + $this->drupalLogout(); + + // Run the submission tests + $this->fileRequiredSubmissionTest($node->nid); + + // Confirm the submission was successful. + $this->assertSubmission($node->nid); + $this->assertFileSubmission($node->nid); + } /** * Confirm multiple files can be submitted by authenticated visitors. @@ -169,7 +160,7 @@ function createTestWebform() { // Delete components that aren't text components, to keep the form simpler. foreach ($node->webform['components'] as $key => $component) { - if ($component['form_key'] != 'textcomponent') { + if ($component['form_key'] != 'textfield') { unset($node->webform['components'][$key]); } } @@ -177,7 +168,7 @@ function createTestWebform() { // Add a file component. $node->webform['components'][] = array( 'name' => 'File', - 'form_key' => 'file', + 'form_key' => 'file_1', 'type' => 'file', 'value' => '', 'pid' => '0', @@ -222,6 +213,12 @@ function createTestWebform() { ); node_save($node); + // Reload the node. + $node = node_load($node->nid); + + // Output a debug version of this node for later. + $this->verbose(print_r($node, TRUE)); + return $node; } @@ -237,9 +234,9 @@ function basicSubmissionTest($nid) { $this->assertResponse(200); // Confirm that the two components are present. - $this->assertFieldByName('submitted[textcomponent]', '', 'Found the text component.'); - $this->assertFieldByName('files[submitted_file]', '', 'Found the file component.'); - $this->assertFieldByName('submitted_file_upload_button', '', 'Found the file component upload button.'); + $this->assertFieldByName('submitted[textfield]', '', 'Found the text component.'); + $this->assertFieldByName('files[submitted_file_1]', '', 'Found the file component.'); + $this->assertFieldByName('submitted_file_1_upload_button', '', 'Found the file component upload button.'); // Submit the form, confirm that the submission went ok. $this->drupalPost('node/' . $nid, array(), 'Submit', array(), array(), 'webform-client-form-' . $nid); @@ -275,7 +272,51 @@ function fileSubmissionTest($nid) { // Submit the webform with a file. $edit = array(); - $edit['files[submitted_file]'] = drupal_realpath($image->uri); + $edit['files[submitted_file_1]'] = drupal_realpath($image->uri); + $this->drupalPost('node/' . $nid, $edit, 'Submit', array(), array(), 'webform-client-form-' . $nid); + $this->assertResponse(200); + $this->assertText(t('Thanks!')); + } + + /** + * Run file tests for a given Webform node with required text field. + * + * @param int $nid + * The node ID to test against. + */ + function fileRequiredSubmissionTest($nid) { + $node = node_load($nid); + + // Make the text field required. + foreach ($node->webform['components'] as &$component) { + if ($component['type'] == 'textfield') { + $component['required'] = '1'; + } + } + node_save($node); + + // Reload the node. + $node = node_load($nid); + + // Generate an example image. + $image = $this->getTestImage(); + + // Submit the webform with a file but also a missing required text field, to + // confirm that the file can still be submitted. + $edit = array(); + $edit['files[submitted_file_1]'] = drupal_realpath($image->uri); + $this->drupalPost('node/' . $nid, $edit, 'Submit', array(), array(), 'webform-client-form-' . $nid); + $this->assertResponse(200); + $this->assertNoText(t('Thanks!')); + + foreach ($node->webform['components'] as $component) { + if ($component['type'] == 'textfield') { + $this->assertText(t('!name field is required.', array('!name' => $component['name']))); + } + } + + // Submit the form again but this time with the missing field. + $edit['submitted[textfield]'] = 'Test submission'; $this->drupalPost('node/' . $nid, $edit, 'Submit', array(), array(), 'webform-client-form-' . $nid); $this->assertResponse(200); $this->assertText(t('Thanks!'));