Updating Media dependent modules to versions compatible with core Media.
[yaffs-website] / web / modules / contrib / media_entity_slideshow / src / Tests / MediaEntitySlideshowTest.php
1 <?php
2
3 namespace Drupal\media_entity_slideshow\Tests;
4
5 use Drupal\Core\Language\Language;
6 use Drupal\file\Entity\File;
7 use Drupal\media\Entity\Media;
8 use Drupal\simpletest\WebTestBase;
9
10 /**
11  * Tests for media entity slideshow.
12  *
13  * @group media_entity_slideshow
14  */
15 class MediaEntitySlideshowTest extends WebTestBase {
16
17   /**
18    * Modules to install.
19    *
20    * @var array
21    */
22   public static $modules = [
23     'media_entity_slideshow_test',
24     'node',
25   ];
26
27   /**
28    * The slideshow media bundle.
29    *
30    * @var \Drupal\media\MediaTypeInterface
31    */
32   protected $slideshowMediaBundle;
33
34   /**
35    * The image media bundle.
36    *
37    * @var \Drupal\media\MediaTypeInterface
38    */
39   protected $imageMediaBundle;
40
41   /**
42    * A collection of media entities, to be used in our test.
43    *
44    * @var \Drupal\media\MediaInterface[]
45    */
46   protected $mediaImageCollection;
47
48   /**
49    * {@inheritdoc}
50    */
51   public function setUp() {
52     parent::setUp();
53     $bundle_storage = $this->container->get('entity_type.manager')->getStorage('media_type');
54     $this->slideshowMediaBundle = $bundle_storage->load('slideshow_bundle');
55     $this->imageMediaBundle = $bundle_storage->load('image_bundle');
56     $adminUser = $this->drupalCreateUser([
57       'view media',
58       'create media',
59       'update media',
60       'update any media',
61       'delete media',
62       'delete any media',
63     ]);
64     $this->drupalLogin($adminUser);
65
66     $this->mediaImageCollection = $this->createMediaImageCollection();
67   }
68
69   /**
70    * Tests media entity slideshow.
71    */
72   public function testMediaEntitySlideshow() {
73
74     // If we have a bundle already the schema is correct.
75     $this->assertTrue((bool) $this->slideshowMediaBundle, 'The media bundle from default configuration has been created in the database.');
76
77     // Test the creation of a media entity of the slidehsow bundle.
78     $this->drupalGet('media/add/' . $this->slideshowMediaBundle->id());
79     $edit = [
80       'name[0][value]' => 'My first slideshow',
81       'field_slides[0][target_id]' => $this->mediaImageCollection[0]->label() . ' (' . $this->mediaImageCollection[0]->id() . ')',
82     ];
83     $this->drupalPostForm(NULL, $edit, t('Save'));
84     $this->assertText('Slideshow bundle My first slideshow has been created', 'Slideshow media entity was correctly created.');
85     $slideshow_id = $this->container->get('entity.query')
86       ->get('media')
87       ->condition('bundle', 'slideshow_bundle')
88       ->sort('created', 'DESC')
89       ->execute();
90     $slideshow = $this->loadMedia(reset($slideshow_id));
91
92     // Add one more slide to it.
93     $this->drupalGet('media/' . $slideshow->id() . '/edit');
94     $edit = [
95       'field_slides[0][target_id]' => $this->mediaImageCollection[0]->label() . ' (' . $this->mediaImageCollection[0]->id() . ')',
96       'field_slides[1][target_id]' => $this->mediaImageCollection[1]->label() . ' (' . $this->mediaImageCollection[1]->id() . ')',
97     ];
98     $this->drupalPostForm(NULL, $edit, t('Save'));
99     $this->assertResponse(200, 'Form submitted correctly');
100     $slideshow = $this->loadMedia($slideshow->id());
101     $this->assertEqual($slideshow->field_slides->count(), 2, 'A new slide was correctly added to the slideshow.');
102
103     // Test removing one of the slides.
104     $this->drupalGet('media/' . $slideshow->id() . '/edit');
105     $edit = [
106       'field_slides[0][target_id]' => $this->mediaImageCollection[0]->label() . ' (' . $this->mediaImageCollection[0]->id() . ')',
107       'field_slides[1][target_id]' => '',
108     ];
109     $this->drupalPostForm(NULL, $edit, t('Save'));
110     $this->assertResponse(200, 'Form submitted correctly');
111     $slideshow = $this->loadMedia($slideshow->id());
112     $this->assertEqual($slideshow->field_slides->count(), 1, 'The deletion of one slide worked properly.');
113
114     // Delete the slideshow entirely.
115     $this->drupalGet('/media/' . $slideshow->id() . '/delete');
116     $this->drupalPostForm(NULL, [], t('Delete'));
117     $this->assertResponse(200, 'Form submitted correctly');
118     $this->assertText('The media My first slideshow has been deleted', 'The slideshow was correctly deleted.');
119   }
120
121   /**
122    * Creates an array of media images to be used in testing.
123    *
124    * @param int $count
125    *   (optional) The number of items to create. Defaults to 3.
126    *
127    * @return \Drupal\media\MediaInterface[]
128    *   An indexed array of fully-loaded media objects of bundle image.
129    */
130   private function createMediaImageCollection($count = 3) {
131     $collection = [];
132     for ($i = 1; $i <= $count; $i++) {
133       $media = Media::create([
134         'bundle' => $this->imageMediaBundle->id(),
135         'name' => 'Image media ' . $i,
136         'uid' => '1',
137         'langcode' => Language::LANGCODE_DEFAULT,
138         'status' => TRUE,
139       ]);
140       $image = $this->getTestFile('image');
141       $media->field_imagefield->target_id = $image->id();
142       $media->save();
143       $collection[] = $media;
144     }
145     return $collection;
146   }
147
148   /**
149    * Load the specified media from the storage.
150    *
151    * @param int $id
152    *   The media identifier.
153    *
154    * @return \Drupal\Core\Entity\EntityInterface
155    *   The loaded media entity.
156    */
157   protected function loadMedia($id) {
158     /** @var \Drupal\Core\Entity\ContentEntityStorageInterface $storage */
159     $storage = $this->container->get('entity_type.manager')->getStorage('media');
160     return $storage->loadUnchanged($id);
161   }
162
163   /**
164    * Retrieves a sample file of the specified type.
165    *
166    * @return \Drupal\file\FileInterface
167    *   A file object recently created and saved.
168    */
169   protected function getTestFile($type_name, $size = NULL) {
170     $file = current($this->drupalGetTestFiles($type_name, $size));
171     $file->filesize = filesize($file->uri);
172     /** @var \Drupal\file\FileInterface $file */
173     $file = File::create((array) $file);
174     $file->setPermanent();
175     $file->save();
176     return $file;
177   }
178
179 }