You are here

function ImageFieldTestBase::uploadNodeImage in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/image/src/Tests/ImageFieldTestBase.php \Drupal\image\Tests\ImageFieldTestBase::uploadNodeImage()

Upload an image to a node.

Parameters

$image: A file object representing the image to upload.

$field_name: Name of the image field the image should be attached to.

$type: The type of node to create.

$alt: The alt text for the image. Use if the field settings require alt text.

12 calls to ImageFieldTestBase::uploadNodeImage()
ImageAdminStylesTest::testConfigImport in core/modules/image/src/Tests/ImageAdminStylesTest.php
Tests image style configuration import that does a delete.
ImageAdminStylesTest::testStyleReplacement in core/modules/image/src/Tests/ImageAdminStylesTest.php
Test deleting a style and choosing a replacement style.
ImageFieldAttributesTest::setUp in core/modules/rdf/src/Tests/ImageFieldAttributesTest.php
Sets up a Drupal site for running functional and integration tests.
ImageFieldDisplayTest::testImageFieldDefaultImage in core/modules/image/src/Tests/ImageFieldDisplayTest.php
Test use of a default image with an image field.
ImageFieldDisplayTest::testImageFieldSettings in core/modules/image/src/Tests/ImageFieldDisplayTest.php
Tests for image field settings.

... See full list

File

core/modules/image/src/Tests/ImageFieldTestBase.php, line 135
Contains \Drupal\image\Tests\ImageFieldTestBase.

Class

ImageFieldTestBase
This class provides methods specifically for testing Image's field handling.

Namespace

Drupal\image\Tests

Code

function uploadNodeImage($image, $field_name, $type, $alt = '') {
  $edit = array(
    'title[0][value]' => $this
      ->randomMachineName(),
  );
  $edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
  $this
    ->drupalPostForm('node/add/' . $type, $edit, t('Save and publish'));
  if ($alt) {

    // Add alt text.
    $this
      ->drupalPostForm(NULL, [
      $field_name . '[0][alt]' => $alt,
    ], t('Save and publish'));
  }

  // Retrieve ID of the newly created node from the current URL.
  $matches = array();
  preg_match('/node\\/([0-9]+)/', $this
    ->getUrl(), $matches);
  return isset($matches[1]) ? $matches[1] : FALSE;
}