annotate core/modules/image/tests/src/Functional/ImageFieldTestBase.php @ 0:4c8ae668cc8c

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