Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / image / tests / src / Functional / ImageFieldTestBase.php
1 <?php
2
3 namespace Drupal\Tests\image\Functional;
4
5 use Drupal\Tests\image\Kernel\ImageFieldCreationTrait;
6 use Drupal\Tests\BrowserTestBase;
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 abstract class ImageFieldTestBase extends BrowserTestBase {
25
26   use ImageFieldCreationTrait;
27
28   /**
29    * Modules to enable.
30    *
31    * @var array
32    */
33   public static $modules = ['node', 'image', 'field_ui', 'image_module_test'];
34
35   /**
36    * An user with permissions to administer content types and image styles.
37    *
38    * @var \Drupal\user\UserInterface
39    */
40   protected $adminUser;
41
42   protected function setUp() {
43     parent::setUp();
44
45     // Create Basic page and Article node types.
46     if ($this->profile != 'standard') {
47       $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
48       $this->drupalCreateContentType(['type' => 'article', 'name' => 'Article']);
49     }
50
51     $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']);
52     $this->drupalLogin($this->adminUser);
53   }
54
55   /**
56    * Preview an image in a node.
57    *
58    * @param \Drupal\Core\Image\ImageInterface $image
59    *   A file object representing the image to upload.
60    * @param string $field_name
61    *   Name of the image field the image should be attached to.
62    * @param string $type
63    *   The type of node to create.
64    */
65   public function previewNodeImage($image, $field_name, $type) {
66     $edit = [
67       'title[0][value]' => $this->randomMachineName(),
68     ];
69     $edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
70     $this->drupalPostForm('node/add/' . $type, $edit, t('Preview'));
71   }
72
73   /**
74    * Upload an image to a node.
75    *
76    * @param $image
77    *   A file object representing the image to upload.
78    * @param $field_name
79    *   Name of the image field the image should be attached to.
80    * @param $type
81    *   The type of node to create.
82    * @param $alt
83    *   The alt text for the image. Use if the field settings require alt text.
84    */
85   public function uploadNodeImage($image, $field_name, $type, $alt = '') {
86     $edit = [
87       'title[0][value]' => $this->randomMachineName(),
88     ];
89     $edit['files[' . $field_name . '_0]'] = drupal_realpath($image->uri);
90     $this->drupalPostForm('node/add/' . $type, $edit, t('Save and publish'));
91     if ($alt) {
92       // Add alt text.
93       $this->drupalPostForm(NULL, [$field_name . '[0][alt]' => $alt], t('Save and publish'));
94     }
95
96     // Retrieve ID of the newly created node from the current URL.
97     $matches = [];
98     preg_match('/node\/([0-9]+)/', $this->getUrl(), $matches);
99     return isset($matches[1]) ? $matches[1] : FALSE;
100   }
101
102   /**
103    * Retrieves the fid of the last inserted file.
104    */
105   protected function getLastFileId() {
106     return (int) db_query('SELECT MAX(fid) FROM {file_managed}')->fetchField();
107   }
108
109 }