1

I have a basic test written (per http://drupal.org/simpletest-tutorial-drupal7) but I am unsure how to proceed to test an image field.

public function testSimpleTestExampleCreate() {
    // Create node to edit.    
    $edit = array();
    $edit['title'] = $this->randomName(8);
    $edit["body[und][0][value]"] = $this->randomName(16);
    $this->drupalPost('node/add/simpletest-example', $edit, t('Save'));
    $this->assertText(t('Simpletest Example Node Type @title has been created.', array('@title' => $edit['title'])));
}

So this will create a node with a title and body. How should I modify this to allow it to test uploading an image as well? Would I just add another key to $edit of 'files[image_image_und_0]' with the path to the image to be uploaded? But I think something else needs to be done since you have to click "Upload" before you save the node.

2

There is a base image field test class in image.module, which has a uploadNodeImage() method. You can either re-use that by extending from that class or steal the code there.

See testStyleReplacment() for an example of how it is used.

Edit: The "Upload" is optional and only necessary if you want to upload multiple files. You can always just save the node directly and any selected files will be uploaded and processed.

| improve this answer | |
  • what do you mean: "You can always just save the node directly and any selected files will be uploaded and processed."? How do I select files? – gregghz Jun 5 '12 at 15:13
  • 1
    That's for yourself, not in the test (which however does the same). you don't need to press the Upload button. The selected image will also be uploaded if you press Save. – Berdir Jun 5 '12 at 15:16
  • ok, but I'm still not clear on what I need to do. Are you suggesting that I set the key 'files[image_und_0]' to the filepath of the image and it will upload? – gregghz Jun 5 '12 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.