diff --git a/src/Type/Media.php b/src/Type/Media.php index 0a6cfe4b..9f5f9525 100644 --- a/src/Type/Media.php +++ b/src/Type/Media.php @@ -53,14 +53,15 @@ public function options($xpath=FALSE) { */ public function processXpath() { $uuids = []; + extract($this->config['options']); + $type = isset($this->config['options']['type']) ? $this->config['options']['type'] : 'media'; $this->crawler->each( - function (Crawler $node) use (&$uuids) { + function (Crawler $node) use (&$uuids, $type) { try { $file = $node->evaluate($this->getOption('file', TRUE)); assert($file->count() > 0); - $file = $file->text(); - $file = $this->getFileUrl($node->text()); + $file = $this->getFileUrl($file->text()); } catch (\Exception $error) { throw new ElementNotFoundException(); } @@ -98,7 +99,6 @@ function (Crawler $node) use (&$uuids) { ); if (count($this->entities) > 0) { - extract($this->config); $this->output->mergeRow("media-{$type}", 'data', $this->entities, TRUE); $this->addValueToRow($uuids); } @@ -111,9 +111,11 @@ function (Crawler $node) use (&$uuids) { */ public function processDom() { $uuids = []; + extract($this->config['options']); + $type = isset($this->config['options']['type']) ? $this->config['options']['type'] : 'media'; $this->crawler->each( - function (Crawler $node) use (&$uuids) { + function (Crawler $node) use (&$uuids, $type) { $name = $node->attr($this->getOption('name')); $file = $node->attr($this->getOption('file')); $file = $this->getFileUrl($file); @@ -140,7 +142,6 @@ function (Crawler $node) use (&$uuids) { ); if (count($this->entities) > 0) { - extract($this->config); $this->output->mergeRow("media-{$type}", 'data', $this->entities, TRUE); $this->addValueToRow($uuids); } diff --git a/tests/Functional/Type/MediaTest.php b/tests/Functional/Type/MediaTest.php index a5287c51..2b080a70 100644 --- a/tests/Functional/Type/MediaTest.php +++ b/tests/Functional/Type/MediaTest.php @@ -5,6 +5,7 @@ use Migrate\Tests\Functional\CrawlerTestCase; use Migrate\Type\Media; use Migrate\Exception\ElementNotFoundException; +use Migrate\Output\OutputBase; class MediaTest extends CrawlerTestCase { @@ -238,4 +239,35 @@ public function testProcessorXpath() $this->assertEquals('alt', $media->entities[0]['alt']); } + /** + * Ensure that media types passed as options build the filename. + */ + public function testFileNames() + { + $config = [ + 'field' => 'field_media', + 'type' => 'media', + 'selector' => '#with-image img', + 'options' => [ + 'name' => 'data-name', + 'file' => 'data-file', + 'alt' => 'data-alt', + 'type' => 'custom-media-type', + ] + ]; + + $output = $this->getOutput(['mergeRow']); + $output->expects($this->once())->method('mergeRow')->with('media-custom-media-type'); + + $row = new \stdClass; + $media = new Media( + $this->getCrawler(), + $output, + $row, + $config + ); + + $media->process(); + } + }