/** * Tests creating a media entity programmatically. */ public function testMediaEntityCreation() { $media = Media::create(['bundle' => $this->testBundle->id(), 'name' => 'Unnamed']); $media->save(); $media_not_exist = (bool) Media::load(rand(1000, 9999)); $this->assertFalse($media_not_exist, 'The media entity does not exist.'); $media_exists = (bool) Media::load($media->id()); $this->assertTrue($media_exists, 'The new media entity has been created in the database.'); }
/** * Upgrade a whole bundle to use video_embed_field. * * @param \Drupal\media_entity\Entity\MediaBundle $bundle * The media bundle object. */ protected function upgradeBundle(MediaBundle $bundle) { // Create a video embed field on the media bundle. VideoEmbedField::createVideoEmbedField($bundle->id()); // Load and update all of the existing media entities. $media_entities = $this->entityQuery->get('media')->condition('bundle', $bundle->id())->execute(); foreach ($media_entities as $media_entity) { $media_entity = Media::load($media_entity); $this->upgradeEntity($media_entity, $bundle->getTypeConfiguration()); } // Update the media bundle type. $bundle->type = 'video_embed_field'; $bundle->save(); }
/** * Test Focal Point change. */ public function testFocalPointChange() { // Media ID used for testing. $mediaId = 9; $page = $this->getSession()->getPage(); $this->drupalGet("media/{$mediaId}/edit"); $this->createScreenshot($this->getScreenshotFolder() . '/MediaImageModifyTest_BeforeFocalPointChange_' . date('Ymd_His') . '.png'); $this->getSession()->getDriver()->executeScript('var e = new jQuery.Event("click"); e.offsetX = 48; e.offsetY = 15; jQuery(".focal-point-wrapper img").trigger(e);'); $this->createScreenshot($this->getScreenshotFolder() . '/MediaImageModifyTest_AfterFocalPointChange_' . date('Ymd_His') . '.png'); $page->pressButton('Save and keep publish'); $media = Media::load($mediaId); $img = $media->get('field_image')->target_id; $file = File::load($img); $path = $file->getFileUri(); $derivativeUri = ImageStyle::load('teaser')->buildUri($path); ImageStyle::load('teaser')->createDerivative($path, $derivativeUri); $image1 = new Imagick($derivativeUri); $image2 = new Imagick(dirname(__FILE__) . '/../../fixtures/reference.jpg'); $result = $image1->compareImages($image2, \Imagick::METRIC_MEANSQUAREERROR); $this->assertTrue($result[1] < 0.01, 'Images are identical'); $image1->clear(); $image2->clear(); }