Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 12 | class GalleryBundleTest extends WebTestBase { |
||
|
|
|||
| 13 | <<<<<<< HEAD |
||
| 14 | /** |
||
| 15 | * Exempt from strict schema checking. |
||
| 16 | * |
||
| 17 | * @see \Drupal\Core\Config\Testing\ConfigSchemaChecker |
||
| 18 | * |
||
| 19 | * @var bool |
||
| 20 | */ |
||
| 21 | protected $strictConfigSchema = FALSE; |
||
| 22 | ======= |
||
| 23 | >>>>>>> 3fead67... Slideshow media bundle |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Modules to enable. |
||
| 27 | * |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | public static $modules = [ |
||
| 31 | 'media', |
||
| 32 | 'media_entity', |
||
| 33 | 'media_entity_slideshow', |
||
| 34 | 'node', |
||
| 35 | ]; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * The test media bundle. |
||
| 39 | * |
||
| 40 | * @var \Drupal\media_entity\MediaBundleInterface |
||
| 41 | */ |
||
| 42 | protected $testBundle; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * {@inheritdoc} |
||
| 46 | */ |
||
| 47 | protected function setUp() { |
||
| 48 | parent::setUp(); |
||
| 49 | <<<<<<< HEAD |
||
| 50 | $this->testBundle = $this->container->get('entity_type.manager')->getStorage('media_bundle')->load('gallery'); |
||
| 51 | ======= |
||
| 52 | $this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('gallery'); |
||
| 53 | >>>>>>> 3fead67... Slideshow media bundle |
||
| 54 | |||
| 55 | $adminUser = $this->drupalCreateUser([ |
||
| 56 | 'view media', |
||
| 57 | 'create media', |
||
| 58 | 'update media', |
||
| 59 | 'update any media', |
||
| 60 | 'delete media', |
||
| 61 | 'delete any media', |
||
| 62 | 'access media overview', |
||
| 63 | ]); |
||
| 64 | $this->drupalLogin($adminUser); |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Tests gallery media bundle creation from config files. |
||
| 69 | */ |
||
| 70 | public function testMediaBundleCreationFromModule() { |
||
| 71 | $type_configuration = [ |
||
| 72 | 'source_field' => 'field_slide', |
||
| 73 | ]; |
||
| 74 | |||
| 75 | $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.'); |
||
| 76 | $this->assertEqual($this->testBundle->get('label'), 'Gallery', 'Correct label detected.'); |
||
| 77 | $this->assertEqual($this->testBundle->get('description'), 'Use Gallery for creating a collection of different media items.', 'Correct description detected.'); |
||
| 78 | $this->assertEqual($this->testBundle->get('type'), 'slideshow', 'Correct plugin ID detected.'); |
||
| 79 | $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.'); |
||
| 80 | $this->assertEqual($this->testBundle->get('field_map'), [], 'Correct field map detected.'); |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Tests thumbnail of the gallery item. |
||
| 85 | */ |
||
| 86 | public function testGalleryItemThumbnail() { |
||
| 87 | // Let's add one image and one video. |
||
| 88 | $imageItem = $this->addImageItem(); |
||
| 89 | $videoItem = $this->addVideoItem(); |
||
| 90 | |||
| 91 | $this->drupalGet('media/add/gallery'); |
||
| 92 | $edit = [ |
||
| 93 | 'name[0][value]' => 'Gallery item', |
||
| 94 | 'field_slide[0][target_id]' => $imageItem['name[0][value]'] . ' (' . $imageItem['id'] . ')', |
||
| 95 | 'field_slide[0][_weight]' => 0, |
||
| 96 | ]; |
||
| 97 | $this->drupalPostAjaxForm(NULL, $edit, 'field_slide_add_more'); |
||
| 98 | $edit['field_slide[1][target_id]'] = $videoItem['name[0][value]'] . ' (' . $videoItem['id'] . ')'; |
||
| 99 | $edit['field_slide[1][_weight]'] = 1; |
||
| 100 | $this->drupalPostForm(NULL, $edit, t('Save and publish')); |
||
| 101 | |||
| 102 | // Let's load all the media items. |
||
| 103 | <<<<<<< HEAD |
||
| 104 | $gallery_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'gallery')->sort('created', 'DESC')->execute(); |
||
| 105 | ======= |
||
| 106 | $gallery_id = \Drupal::entityQuery('media')->condition('bundle', 'gallery')->sort('created', 'DESC')->execute(); |
||
| 107 | >>>>>>> 3fead67... Slideshow media bundle |
||
| 108 | $gallery = $this->loadMediaItem(reset($gallery_id)); |
||
| 109 | $image = $this->loadMediaItem($imageItem['id']); |
||
| 110 | $video = $this->loadMediaItem($videoItem['id']); |
||
| 111 | // Let's check thumbnail now. |
||
| 112 | $gallery_thumbnail = $gallery->getType()->thumbnail($gallery); |
||
| 113 | $image_thumbnail = $image->getType()->thumbnail($image); |
||
| 114 | $video_thumbnail = $video->getType()->thumbnail($video); |
||
| 115 | $this->assertEqual($gallery_thumbnail, $image_thumbnail, "Correct thumbnail detected."); |
||
| 116 | |||
| 117 | $this->drupalGet('media/add/gallery'); |
||
| 118 | $edit = [ |
||
| 119 | 'name[0][value]' => 'Gallery item 2', |
||
| 120 | 'field_slide[0][target_id]' => $videoItem['name[0][value]'] . ' (' . $videoItem['id'] . ')', |
||
| 121 | 'field_slide[0][_weight]' => 0, |
||
| 122 | ]; |
||
| 123 | $this->drupalPostAjaxForm(NULL, $edit, 'field_slide_add_more'); |
||
| 124 | $edit['field_slide[1][target_id]'] = $imageItem['name[0][value]'] . ' (' . $imageItem['id'] . ')'; |
||
| 125 | $edit['field_slide[1][_weight]'] = 1; |
||
| 126 | $this->drupalPostForm(NULL, $edit, t('Save and publish')); |
||
| 127 | |||
| 128 | // Let's check the thumbnail again. |
||
| 129 | <<<<<<< HEAD |
||
| 130 | $gallery_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'gallery')->sort('created', 'DESC')->execute(); |
||
| 131 | ======= |
||
| 132 | $gallery_id = \Drupal::entityQuery('media')->condition('bundle', 'gallery')->sort('created', 'DESC')->execute(); |
||
| 133 | >>>>>>> 3fead67... Slideshow media bundle |
||
| 134 | $gallery = $this->loadMediaItem(reset($gallery_id)); |
||
| 135 | $gallery_thumbnail = $gallery->getType()->thumbnail($gallery); |
||
| 136 | $this->assertEqual($gallery_thumbnail, $video_thumbnail, "Correct thumbnail detected."); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Adds image type item. |
||
| 141 | */ |
||
| 142 | public function addImageItem() { |
||
| 143 | // Let's add image first. |
||
| 144 | $name = $this->randomMachineName(); |
||
| 145 | $testImage = current($this->drupalGetTestFiles('image')); |
||
| 146 | $file_path = $this->container->get('file_system')->realpath($testImage->uri); |
||
| 147 | $edit = [ |
||
| 148 | 'name[0][value]' => $name, |
||
| 149 | 'files[field_image_0]' => $file_path, |
||
| 150 | ]; |
||
| 151 | // Save the image. |
||
| 152 | $this->drupalPostForm('media/add/image', $edit, t('Save and publish')); |
||
| 153 | $this->drupalPostForm(NULL, ['field_image[0][alt]' => $name], t('Save and publish')); |
||
| 154 | // Obtain the image id. |
||
| 155 | <<<<<<< HEAD |
||
| 156 | $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'image')->sort('created', 'DESC')->execute(); |
||
| 157 | ======= |
||
| 158 | $media_id = \Drupal::entityQuery('media')->condition('bundle', 'image')->sort('created', 'DESC')->execute(); |
||
| 159 | >>>>>>> 3fead67... Slideshow media bundle |
||
| 160 | $media_id = reset($media_id); |
||
| 161 | $edit['id'] = $media_id; |
||
| 162 | |||
| 163 | return $edit; |
||
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * Adds video type item. |
||
| 168 | */ |
||
| 169 | public function addVideoItem() { |
||
| 170 | $edit = [ |
||
| 171 | 'name[0][value]' => 'Drupal video!', |
||
| 172 | 'field_video[0][value]' => 'https://www.youtube.com/watch?v=XgYu7-DQjDQ', |
||
| 173 | ]; |
||
| 174 | $this->drupalPostForm('media/add/video', $edit, t('Save and publish')); |
||
| 175 | // Obtain the video id. |
||
| 176 | <<<<<<< HEAD |
||
| 177 | $media_id = $this->container->get('entity.query')->get('media')->condition('bundle', 'video')->sort('created', 'DESC')->execute(); |
||
| 178 | ======= |
||
| 179 | $media_id = \Drupal::entityQuery('media')->condition('bundle', 'video')->sort('created', 'DESC')->execute(); |
||
| 180 | >>>>>>> 3fead67... Slideshow media bundle |
||
| 181 | $media_id = reset($media_id); |
||
| 204 |