ImageFieldTestBase.php 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
<?php

namespace Drupal\Tests\image\Functional;

use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
use Drupal\Tests\BrowserTestBase;

/**
 * TODO: Test the following functions.
 *
11 12
 * In file:
 * - image.effects.inc:
13 14 15
 *   image_style_generate()
 *   \Drupal\image\ImageStyleInterface::createDerivative()
 *
16
 * - image.module:
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *   image_style_options()
 *   \Drupal\image\ImageStyleInterface::flush()
 *   image_filter_keyword()
 */

/**
 * This class provides methods specifically for testing Image's field handling.
 */
abstract class ImageFieldTestBase extends BrowserTestBase {

  use ImageFieldCreationTrait;

  /**
   * Modules to enable.
   *
   * @var array
   */
34 35 36 37 38 39
  protected static $modules = [
    'node',
    'image',
    'field_ui',
    'image_module_test',
  ];
40 41

  /**
42
   * A user with permissions to administer content types and image styles.
43 44 45 46 47 48 49 50 51 52
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

  protected function setUp() {
    parent::setUp();

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

57 58 59 60 61 62 63 64 65 66 67 68 69
    $this->adminUser = $this->drupalCreateUser([
      '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',
      'administer node display',
    ]);
70 71 72 73 74 75 76 77 78 79 80 81 82
    $this->drupalLogin($this->adminUser);
  }

  /**
   * 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.
   */
83
  public function previewNodeImage($image, $field_name, $type) {
84
    $edit = [
85
      'title[0][value]' => $this->randomMachineName(),
86
    ];
87
    $edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri);
88
    $this->drupalPostForm('node/add/' . $type, $edit, 'Preview');
89 90 91 92 93 94 95 96 97 98 99 100 101 102
  }

  /**
   * 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.
   * @param $alt
   *   The alt text for the image. Use if the field settings require alt text.
   */
103
  public function uploadNodeImage($image, $field_name, $type, $alt = '') {
104
    $edit = [
105
      'title[0][value]' => $this->randomMachineName(),
106
    ];
107
    $edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri);
108
    $this->drupalPostForm('node/add/' . $type, $edit, 'Save');
109 110
    if ($alt) {
      // Add alt text.
111
      $this->submitForm([$field_name . '[0][alt]' => $alt], 'Save');
112 113 114
    }

    // Retrieve ID of the newly created node from the current URL.
115
    $matches = [];
116 117 118 119 120 121 122 123
    preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
    return isset($matches[1]) ? $matches[1] : FALSE;
  }

  /**
   * Retrieves the fid of the last inserted file.
   */
  protected function getLastFileId() {
124 125 126 127
    return (int) \Drupal::entityQueryAggregate('file')
      ->accessCheck(FALSE)
      ->aggregate('fid', 'max')
      ->execute()[0]['fid_max'];
128 129 130
  }

}