annotate core/modules/image/tests/src/Functional/ImageFieldTestBase.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\image\Functional;
Chris@0 4
Chris@0 5 use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
Chris@0 6 use Drupal\Tests\BrowserTestBase;
Chris@0 7
Chris@0 8 /**
Chris@0 9 * TODO: Test the following functions.
Chris@0 10 *
Chris@17 11 * In file:
Chris@17 12 * - image.effects.inc:
Chris@0 13 * image_style_generate()
Chris@0 14 * \Drupal\image\ImageStyleInterface::createDerivative()
Chris@0 15 *
Chris@17 16 * - image.module:
Chris@0 17 * image_style_options()
Chris@0 18 * \Drupal\image\ImageStyleInterface::flush()
Chris@0 19 * image_filter_keyword()
Chris@0 20 */
Chris@0 21
Chris@0 22 /**
Chris@0 23 * This class provides methods specifically for testing Image's field handling.
Chris@0 24 */
Chris@0 25 abstract class ImageFieldTestBase extends BrowserTestBase {
Chris@0 26
Chris@0 27 use ImageFieldCreationTrait;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * Modules to enable.
Chris@0 31 *
Chris@0 32 * @var array
Chris@0 33 */
Chris@0 34 public static $modules = ['node', 'image', 'field_ui', 'image_module_test'];
Chris@0 35
Chris@0 36 /**
Chris@0 37 * An user with permissions to administer content types and image styles.
Chris@0 38 *
Chris@0 39 * @var \Drupal\user\UserInterface
Chris@0 40 */
Chris@0 41 protected $adminUser;
Chris@0 42
Chris@0 43 protected function setUp() {
Chris@0 44 parent::setUp();
Chris@0 45
Chris@0 46 // Create Basic page and Article node types.
Chris@0 47 if ($this->profile != 'standard') {
Chris@0 48 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
Chris@0 49 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
Chris@0 50 }
Chris@0 51
Chris@0 52 $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']);
Chris@0 53 $this->drupalLogin($this->adminUser);
Chris@0 54 }
Chris@0 55
Chris@0 56 /**
Chris@0 57 * Preview an image in a node.
Chris@0 58 *
Chris@0 59 * @param \Drupal\Core\Image\ImageInterface $image
Chris@0 60 * A file object representing the image to upload.
Chris@0 61 * @param string $field_name
Chris@0 62 * Name of the image field the image should be attached to.
Chris@0 63 * @param string $type
Chris@0 64 * The type of node to create.
Chris@0 65 */
Chris@0 66 public function previewNodeImage($image, $field_name, $type) {
Chris@0 67 $edit = [
Chris@0 68 'title[0][value]' => $this->randomMachineName(),
Chris@0 69 ];
Chris@14 70 $edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri);
Chris@0 71 $this->drupalPostForm('node/add/' . $type, $edit, t('Preview'));
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * Upload an image to a node.
Chris@0 76 *
Chris@0 77 * @param $image
Chris@0 78 * A file object representing the image to upload.
Chris@0 79 * @param $field_name
Chris@0 80 * Name of the image field the image should be attached to.
Chris@0 81 * @param $type
Chris@0 82 * The type of node to create.
Chris@0 83 * @param $alt
Chris@0 84 * The alt text for the image. Use if the field settings require alt text.
Chris@0 85 */
Chris@0 86 public function uploadNodeImage($image, $field_name, $type, $alt = '') {
Chris@0 87 $edit = [
Chris@0 88 'title[0][value]' => $this->randomMachineName(),
Chris@0 89 ];
Chris@14 90 $edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri);
Chris@16 91 $this->drupalPostForm('node/add/' . $type, $edit, t('Save'));
Chris@0 92 if ($alt) {
Chris@0 93 // Add alt text.
Chris@16 94 $this->drupalPostForm(NULL, [$field_name . '[0][alt]' => $alt], t('Save'));
Chris@0 95 }
Chris@0 96
Chris@0 97 // Retrieve ID of the newly created node from the current URL.
Chris@0 98 $matches = [];
Chris@0 99 preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
Chris@0 100 return isset($matches[1]) ? $matches[1] : FALSE;
Chris@0 101 }
Chris@0 102
Chris@0 103 /**
Chris@0 104 * Retrieves the fid of the last inserted file.
Chris@0 105 */
Chris@0 106 protected function getLastFileId() {
Chris@18 107 return (int) \Drupal::entityQueryAggregate('file')->aggregate('fid', 'max')->execute()[0]['fid_max'];
Chris@0 108 }
Chris@0 109
Chris@0 110 }