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