3 namespace Drupal\image\Tests;
5 use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
6 use Drupal\simpletest\WebTestBase;
9 * TODO: Test the following functions.
12 * image_style_generate()
13 * \Drupal\image\ImageStyleInterface::createDerivative()
16 * image_style_options()
17 * \Drupal\image\ImageStyleInterface::flush()
18 * image_filter_keyword()
22 * This class provides methods specifically for testing Image's field handling.
24 * @deprecated Scheduled for removal in Drupal 9.0.0.
25 * Use \Drupal\Tests\image\Functional\ImageFieldTestBase instead.
27 abstract class ImageFieldTestBase extends WebTestBase {
29 use ImageFieldCreationTrait;
36 public static $modules = ['node', 'image', 'field_ui', 'image_module_test'];
39 * An user with permissions to administer content types and image styles.
41 * @var \Drupal\user\UserInterface
45 protected function setUp() {
48 // Create Basic page and Article node types.
49 if ($this->profile != 'standard') {
50 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
51 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
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']);
55 $this->drupalLogin($this->adminUser);
59 * Preview an image in a node.
61 * @param \Drupal\Core\Image\ImageInterface $image
62 * A file object representing the image to upload.
63 * @param string $field_name
64 * Name of the image field the image should be attached to.
66 * The type of node to create.
68 public function previewNodeImage($image, $field_name, $type) {
70 'title[0][value]' => $this->randomMachineName(),
72 $edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
73 $this->drupalPostForm('node/add/' . $type, $edit, t('Preview'));
77 * Upload an image to a node.
80 * A file object representing the image to upload.
82 * Name of the image field the image should be attached to.
84 * The type of node to create.
86 * The alt text for the image. Use if the field settings require alt text.
88 public function uploadNodeImage($image, $field_name, $type, $alt = '') {
90 'title[0][value]' => $this->randomMachineName(),
92 $edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
93 $this->drupalPostForm('node/add/' . $type, $edit, t('Save and publish'));
96 $this->drupalPostForm(NULL, [$field_name . '[0][alt]' => $alt], t('Save and publish'));
99 // Retrieve ID of the newly created node from the current URL.
101 preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
102 return isset($matches[1]) ? $matches[1] : FALSE;
106 * Retrieves the fid of the last inserted file.
108 protected function getLastFileId() {
109 return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();