View source
<?php
namespace Drupal\Tests\migrate\Unit\process;
use Drupal\migrate\Plugin\migrate\process\DedupeEntity;
use Drupal\Component\Utility\Unicode;
class DedupeEntityTest extends MigrateProcessTestCase {
protected $entityQuery;
protected $entityQueryFactory;
protected $migrationConfiguration = array(
'id' => 'test',
);
protected function setUp() {
$this->entityQuery = $this
->getMockBuilder('Drupal\\Core\\Entity\\Query\\QueryInterface')
->disableOriginalConstructor()
->getMock();
$this->entityQueryFactory = $this
->getMockBuilder('Drupal\\Core\\Entity\\Query\\QueryFactory')
->disableOriginalConstructor()
->getMock();
$this->entityQueryFactory
->expects($this
->any())
->method('get')
->will($this
->returnValue($this->entityQuery));
parent::setUp();
}
public function testDedupe($count, $postfix = '', $start = NULL, $length = NULL) {
$configuration = array(
'entity_type' => 'test_entity_type',
'field' => 'test_field',
);
if ($postfix) {
$configuration['postfix'] = $postfix;
}
$configuration['start'] = isset($start) ? $start : NULL;
$configuration['length'] = isset($length) ? $length : NULL;
$plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this
->getMigration(), $this->entityQueryFactory);
$this
->entityQueryExpects($count);
$value = $this
->randomMachineName(32);
$actual = $plugin
->transform($value, $this->migrateExecutable, $this->row, 'testproperty');
$expected = Unicode::substr($value, $start, $length);
$expected .= $count ? $postfix . $count : '';
$this
->assertSame($expected, $actual);
}
public function testDedupeEntityInvalidStart() {
$configuration = array(
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'start' => 'foobar',
);
$plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this
->getMigration(), $this->entityQueryFactory);
$this
->setExpectedException('Drupal\\migrate\\MigrateException', 'The start position configuration key should be an integer. Omit this key to capture from the beginning of the string.');
$plugin
->transform('test_start', $this->migrateExecutable, $this->row, 'testproperty');
}
public function testDedupeEntityInvalidLength() {
$configuration = array(
'entity_type' => 'test_entity_type',
'field' => 'test_field',
'length' => 'foobar',
);
$plugin = new DedupeEntity($configuration, 'dedupe_entity', array(), $this
->getMigration(), $this->entityQueryFactory);
$this
->setExpectedException('Drupal\\migrate\\MigrateException', 'The character length configuration key should be an integer. Omit this key to capture the entire string.');
$plugin
->transform('test_length', $this->migrateExecutable, $this->row, 'testproperty');
}
public function providerTestDedupe() {
return array(
array(
0,
),
array(
0,
NULL,
10,
),
array(
0,
NULL,
5,
10,
),
array(
0,
NULL,
NULL,
10,
),
array(
3,
),
array(
3,
NULL,
10,
),
array(
3,
NULL,
5,
10,
),
array(
3,
NULL,
NULL,
10,
),
array(
0,
'_',
),
array(
0,
'_',
5,
),
array(
0,
'_',
5,
10,
),
array(
0,
'_',
NULL,
10,
),
array(
2,
'_',
),
array(
2,
'_',
5,
),
array(
2,
'_',
5,
10,
),
array(
2,
'_',
NULL,
10,
),
);
}
protected function entityQueryExpects($count) {
$this->entityQuery
->expects($this
->exactly($count + 1))
->method('condition')
->will($this
->returnValue($this->entityQuery));
$this->entityQuery
->expects($this
->exactly($count + 1))
->method('count')
->will($this
->returnValue($this->entityQuery));
$this->entityQuery
->expects($this
->exactly($count + 1))
->method('execute')
->will($this
->returnCallback(function () use (&$count) {
return $count--;
}));
}
}