annotate core/modules/image/src/Tests/ImageFieldTestBase.php @ 16:c2387f117808

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