228b1d6f050ce8216d1b1942643a3294ebf5c47b
[yaffs-website] / web / core / modules / file / tests / src / Functional / Formatter / FileMediaFormatterTestBase.php
1 <?php
2
3 namespace Drupal\Tests\file\Functional\Formatter;
4
5 use Drupal\Core\Field\FieldStorageDefinitionInterface;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\Tests\BrowserTestBase;
9
10 /**
11  * Provides methods specifically for testing File module's media formatter's.
12  */
13 abstract class FileMediaFormatterTestBase extends BrowserTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected static $modules = [
19     'entity_test',
20     'field',
21     'file',
22     'user',
23     'system',
24   ];
25
26   /**
27    * {@inheritdoc}
28    */
29   protected function setUp() {
30     parent::setUp();
31     $this->drupalLogin($this->drupalCreateUser(['view test entity']));
32   }
33
34   /**
35    * Creates a file field and set's the correct formatter.
36    *
37    * @param string $formatter
38    *   The formatter ID.
39    * @param string $file_extensions
40    *   The file extensions of the new field.
41    * @param array $formatter_settings
42    *   Settings for the formatter.
43    *
44    * @return \Drupal\field\Entity\FieldConfig
45    *   Newly created file field.
46    */
47   protected function createMediaField($formatter, $file_extensions, array $formatter_settings = []) {
48     $entity_type = $bundle = 'entity_test';
49     $field_name = mb_strtolower($this->randomMachineName());
50
51     FieldStorageConfig::create([
52       'entity_type' => $entity_type,
53       'field_name' => $field_name,
54       'type' => 'file',
55       'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
56     ])->save();
57     $field_config = FieldConfig::create([
58       'entity_type' => $entity_type,
59       'field_name' => $field_name,
60       'bundle' => $bundle,
61       'settings' => [
62         'file_extensions' => trim($file_extensions),
63       ],
64     ]);
65     $field_config->save();
66
67     $display = entity_get_display('entity_test', 'entity_test', 'full');
68     $display->setComponent($field_name, [
69       'type' => $formatter,
70       'settings' => $formatter_settings,
71     ])->save();
72
73     return $field_config;
74   }
75
76   /**
77    * Data provider for testRender.
78    *
79    * @return array
80    *   An array of data arrays.
81    *   The data array contains:
82    *     - The number of expected HTML tags.
83    *     - An array of settings for the field formatter.
84    */
85   public function dataProvider() {
86     return [
87       [2, []],
88       [1, ['multiple_file_display_type' => 'sources']],
89     ];
90   }
91
92 }