| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace Drupal\media\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Drupal\simpletest\WebTestBase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Ensures that media bundle for videos can be created. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @group media | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | class VideoBundleTest extends WebTestBase { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |    * Modules to enable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |    * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |   public static $modules = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     'media', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     'media_entity', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     'video_embed_field', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |     'video_embed_media', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     'node', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |   ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |    * The test media bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |    * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |    * @var \Drupal\media_entity\MediaBundleInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |   protected $testBundle; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |    * {@inheritdoc} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |    */ | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 37 |  | View Code Duplication |   protected function setUp() { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     parent::setUp(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     $this->testBundle = $this->container->get('entity.manager')->getStorage('media_bundle')->load('video'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     $adminUser = $this->drupalCreateUser([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |       'view media', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |       'create media', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |       'update media', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |       'update any media', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |       'delete media', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |       'delete any media', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |       'access media overview', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     $this->drupalLogin($adminUser); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |    * Tests video media bundle creation from config files. | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 55 |  |  |    */ | 
            
                                                                        
                            
            
                                    
            
            
                | 56 |  |  |   public function testMediaBundleCreationFromModule() { | 
            
                                                                        
                            
            
                                    
            
            
                | 57 |  |  |     $type_configuration = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 58 |  |  |       'source_field' => 'field_video', | 
            
                                                                        
                            
            
                                    
            
            
                | 59 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 61 |  |  |     $field_map = [ | 
            
                                                                        
                            
            
                                    
            
            
                | 62 |  |  |       'id' => 'field_id', | 
            
                                                                        
                            
            
                                    
            
            
                | 63 |  |  |       'source_name' => 'field_source', | 
            
                                                                        
                            
            
                                    
            
            
                | 64 |  |  |     ]; | 
            
                                                                        
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 66 |  |  |     $this->assertTrue((bool) $this->testBundle, 'The media bundle from default configuration has been created in the database.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 67 |  |  |     $this->assertEqual($this->testBundle->get('label'), 'Video', 'Correct label detected.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 68 |  |  |     $this->assertEqual($this->testBundle->get('description'), 'Use Video for embedding videos hosted by YouTube, Vimeo, or some other provider.', 'Correct description detected.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 69 |  |  |     $this->assertEqual($this->testBundle->get('type'), 'video_embed_field', 'Correct plugin ID detected.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 70 |  |  |     $this->assertEqual($this->testBundle->get('type_configuration'), $type_configuration, 'Type configuration correct.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     $this->assertEqual($this->testBundle->get('field_map'), $field_map, 'Correct field map detected.'); | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |   /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |    * Tests video media bundle field maps. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |    */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |   public function testBundleFieldMap() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     $edit = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |       'name[0][value]' => 'Drupal video!', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |       'field_video[0][value]' => 'https://www.youtube.com/watch?v=XgYu7-DQjDQ', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     $this->drupalPostForm('media/add/' . $this->testBundle->id(), $edit, t('Save and publish')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     // Let's retrieve the media id and corresponding media entity object. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     $media_id = $this->container->get('entity.query')->get('media')->execute(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     $media_id = reset($media_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** @var \Drupal\media_entity\MediaInterface $media */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     $media = $this->container->get('entity_type.manager') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |       ->getStorage('media') | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |       ->loadUnchanged($media_id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     $properties = $media->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |     $this->assertEqual($properties['field_id'][0]['value'], 'XgYu7-DQjDQ', 'Correct video ID detected.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     $this->assertEqual($properties['field_source'][0]['value'], 'youtube', 'Correct video source detected.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |   } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 96 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 97 |  |  |  | 
            
                        
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.