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