Skip to content

Commit

Permalink
Merge pull request #80 from salsadigitalauorg/feature/media-type-file
Browse files Browse the repository at this point in the history
Ensure that media types are added to output filenames.
  • Loading branch information
SRowlands authored Sep 5, 2019
2 parents d044d41 + c9ebfda commit ccad69c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/Type/Media.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
Expand All @@ -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);
}
Expand Down
32 changes: 32 additions & 0 deletions tests/Functional/Type/MediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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();
}

}

0 comments on commit ccad69c

Please sign in to comment.