3 namespace Drupal\image\Tests;
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);
7 use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
8 use Drupal\simpletest\WebTestBase;
11 * TODO: Test the following functions.
14 * - image.effects.inc:
15 * image_style_generate()
16 * \Drupal\image\ImageStyleInterface::createDerivative()
19 * image_style_options()
20 * \Drupal\image\ImageStyleInterface::flush()
21 * image_filter_keyword()
25 * This class provides methods specifically for testing Image's field handling.
27 * @deprecated Scheduled for removal in Drupal 9.0.0.
28 * Use \Drupal\Tests\image\Functional\ImageFieldTestBase instead.
30 abstract class ImageFieldTestBase extends WebTestBase {
32 use ImageFieldCreationTrait;
39 public static $modules = ['node', 'image', 'field_ui', 'image_module_test'];
42 * An user with permissions to administer content types and image styles.
44 * @var \Drupal\user\UserInterface
48 protected function setUp() {
51 // Create Basic page and Article node types.
52 if ($this->profile != 'standard') {
53 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
54 $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
57 $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']);
58 $this->drupalLogin($this->adminUser);
62 * Preview an image in a node.
64 * @param \Drupal\Core\Image\ImageInterface $image
65 * A file object representing the image to upload.
66 * @param string $field_name
67 * Name of the image field the image should be attached to.
69 * The type of node to create.
71 public function previewNodeImage($image, $field_name, $type) {
73 'title[0][value]' => $this->randomMachineName(),
75 $edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri);
76 $this->drupalPostForm('node/add/' . $type, $edit, t('Preview'));
80 * Upload an image to a node.
83 * A file object representing the image to upload.
85 * Name of the image field the image should be attached to.
87 * The type of node to create.
89 * The alt text for the image. Use if the field settings require alt text.
91 public function uploadNodeImage($image, $field_name, $type, $alt = '') {
93 'title[0][value]' => $this->randomMachineName(),
95 $edit['files[' . $field_name . '_0]'] = \Drupal::service('file_system')->realpath($image->uri);
96 $this->drupalPostForm('node/add/' . $type, $edit, t('Save'));
99 $this->drupalPostForm(NULL, [$field_name . '[0][alt]' => $alt], t('Save'));
102 // Retrieve ID of the newly created node from the current URL.
104 preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
105 return isset($matches[1]) ? $matches[1] : FALSE;
109 * Retrieves the fid of the last inserted file.
111 protected function getLastFileId() {
112 return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();