annotate core/modules/image/src/Tests/ImageFieldTestBase.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children c2387f117808
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\image\Tests;
Chris@0 4
Chris@0 5 use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
Chris@0 6 use Drupal\simpletest\WebTestBase;
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 * @deprecated Scheduled for removal in Drupal 9.0.0.
Chris@0 25 * Use \Drupal\Tests\image\Functional\ImageFieldTestBase instead.
Chris@0 26 */
Chris@0 27 abstract class ImageFieldTestBase extends WebTestBase {
Chris@0 28
Chris@0 29 use ImageFieldCreationTrait;
Chris@0 30
Chris@0 31 /**
Chris@0 32 * Modules to enable.
Chris@0 33 *
Chris@0 34 * @var array
Chris@0 35 */
Chris@0 36 public static $modules = ['node', 'image', 'field_ui', 'image_module_test'];
Chris@0 37
Chris@0 38 /**
Chris@0 39 * An user with permissions to administer content types and image styles.
Chris@0 40 *
Chris@0 41 * @var \Drupal\user\UserInterface
Chris@0 42 */
Chris@0 43 protected $adminUser;
Chris@0 44
Chris@0 45 protected function setUp() {
Chris@0 46 parent::setUp();
Chris@0 47
Chris@0 48 // Create Basic page and Article node types.
Chris@0 49 if ($this->profile != 'standard') {
Chris@0 50 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
Chris@0 51 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
Chris@0 52 }
Chris@0 53
Chris@0 54 $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 55 $this->drupalLogin($this->adminUser);
Chris@0 56 }
Chris@0 57
Chris@0 58 /**
Chris@0 59 * Preview an image in a node.
Chris@0 60 *
Chris@0 61 * @param \Drupal\Core\Image\ImageInterface $image
Chris@0 62 * A file object representing the image to upload.
Chris@0 63 * @param string $field_name
Chris@0 64 * Name of the image field the image should be attached to.
Chris@0 65 * @param string $type
Chris@0 66 * The type of node to create.
Chris@0 67 */
Chris@0 68 public function previewNodeImage($image, $field_name, $type) {
Chris@0 69 $edit = [
Chris@0 70 'title[0][value]' => $this->randomMachineName(),
Chris@0 71 ];
Chris@14 72 $edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri);
Chris@0 73 $this->drupalPostForm('node/add/' . $type, $edit, t('Preview'));
Chris@0 74 }
Chris@0 75
Chris@0 76 /**
Chris@0 77 * Upload an image to a node.
Chris@0 78 *
Chris@0 79 * @param $image
Chris@0 80 * A file object representing the image to upload.
Chris@0 81 * @param $field_name
Chris@0 82 * Name of the image field the image should be attached to.
Chris@0 83 * @param $type
Chris@0 84 * The type of node to create.
Chris@0 85 * @param $alt
Chris@0 86 * The alt text for the image. Use if the field settings require alt text.
Chris@0 87 */
Chris@0 88 public function uploadNodeImage($image, $field_name, $type, $alt = '') {
Chris@0 89 $edit = [
Chris@0 90 'title[0][value]' => $this->randomMachineName(),
Chris@0 91 ];
Chris@14 92 $edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri);
Chris@0 93 $this->drupalPostForm('node/add/' . $type, $edit, t('Save'));
Chris@0 94 if ($alt) {
Chris@0 95 // Add alt text.
Chris@0 96 $this->drupalPostForm(NULL, [$field_name . '[0][alt]' => $alt], t('Save'));
Chris@0 97 }
Chris@0 98
Chris@0 99 // Retrieve ID of the newly created node from the current URL.
Chris@0 100 $matches = [];
Chris@0 101 preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
Chris@0 102 return isset($matches[1]) ? $matches[1] : FALSE;
Chris@0 103 }
Chris@0 104
Chris@0 105 /**
Chris@0 106 * Retrieves the fid of the last inserted file.
Chris@0 107 */
Chris@0 108 protected function getLastFileId() {
Chris@0 109 return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
Chris@0 110 }
Chris@0 111
Chris@0 112 }