ImageFieldTestBase.php 4.44 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
<?php

/**
 * @file
 * Definition of Drupal\image\Tests\ImageFieldTestBase.
 */

namespace Drupal\image\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * TODO: Test the following functions.
 *
 * image.effects.inc:
 *   image_style_generate()
17
 *   \Drupal\image\ImageStyleInterface::createDerivative()
18 19 20
 *
 * image.module:
 *   image_style_options()
21
 *   \Drupal\image\ImageStyleInterface::flush()
22 23 24 25 26 27
 *   image_filter_keyword()
 */

/**
 * This class provides methods specifically for testing Image's field handling.
 */
28
abstract class ImageFieldTestBase extends WebTestBase {
29 30 31 32 33 34

  /**
   * Modules to enable.
   *
   * @var array
   */
35
  public static $modules = array('node', 'image', 'field_ui', 'image_module_test');
36

37 38
  protected $admin_user;

39
  protected function setUp() {
40
    parent::setUp();
41 42 43 44 45 46 47

    // Create Basic page and Article node types.
    if ($this->profile != 'standard') {
      $this->drupalCreateContentType(array('type' => 'page', 'name' => 'Basic page'));
      $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
    }

48
    $this->admin_user = $this->drupalCreateUser(array('access content', 'access administration pages', 'administer site configuration', 'administer content types', 'administer node fields', 'administer nodes', 'create article content', 'edit any article content', 'delete any article content', 'administer image styles'));
49 50 51 52 53 54 55 56 57 58
    $this->drupalLogin($this->admin_user);
  }

  /**
   * Create a new image field.
   *
   * @param $name
   *   The name of the new field (all lowercase), exclude the "field_" prefix.
   * @param $type_name
   *   The node type that this field will be added to.
59 60
   * @param $storage_settings
   *   A list of field storage settings that will be added to the defaults.
61 62 63 64 65
   * @param $instance_settings
   *   A list of instance settings that will be added to the instance defaults.
   * @param $widget_settings
   *   A list of widget settings that will be added to the widget defaults.
   */
66 67
  function createImageField($name, $type_name, $storage_settings = array(), $instance_settings = array(), $widget_settings = array()) {
    entity_create('field_storage_config', array(
68 69
      'name' => $name,
      'entity_type' => 'node',
70
      'type' => 'image',
71 72
      'settings' => $storage_settings,
      'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1,
73
    ))->save();
74

75 76
    $field_instance_config = entity_create('field_instance_config', array(
      'field_name' => $name,
77
      'label' => $name,
78
      'entity_type' => 'node',
79 80
      'bundle' => $type_name,
      'required' => !empty($instance_settings['required']),
81
      'description' => !empty($instance_settings['description']) ? $instance_settings['description'] : '',
82 83 84
      'settings' => $instance_settings,
    ));
    $field_instance_config->save();
85

86
    entity_get_form_display('node', $type_name, 'default')
87
      ->setComponent($name, array(
88 89 90 91 92
        'type' => 'image_image',
        'settings' => $widget_settings,
      ))
      ->save();

93
    entity_get_display('node', $type_name, 'default')
94
      ->setComponent($name)
95 96
      ->save();

97
    return $field_instance_config;
98

99 100
  }

101 102 103 104 105 106 107 108 109 110 111 112
  /**
   * Preview an image in a node.
   *
   * @param \Drupal\Core\Image\ImageInterface $image
   *   A file object representing the image to upload.
   * @param string $field_name
   *   Name of the image field the image should be attached to.
   * @param string $type
   *   The type of node to create.
   */
  function previewNodeImage($image, $field_name, $type) {
    $edit = array(
113
      'title[0][value]' => $this->randomMachineName(),
114 115 116 117 118
    );
    $edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
    $this->drupalPostForm('node/add/' . $type, $edit, t('Preview'));
  }

119 120 121 122 123 124 125 126 127 128 129 130
  /**
   * Upload an image to a node.
   *
   * @param $image
   *   A file object representing the image to upload.
   * @param $field_name
   *   Name of the image field the image should be attached to.
   * @param $type
   *   The type of node to create.
   */
  function uploadNodeImage($image, $field_name, $type) {
    $edit = array(
131
      'title[0][value]' => $this->randomMachineName(),
132
    );
133
    $edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
134
    $this->drupalPostForm('node/add/' . $type, $edit, t('Save and publish'));
135 136 137 138 139 140

    // 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;
  }
141

142
}