diff --git a/src/DataParserPluginBase.php b/src/DataParserPluginBase.php index c74c6c53acbefb03f4812335145229d00fc3cead..692942b00c39a853dd003964859738b21b56b1db 100644 --- a/src/DataParserPluginBase.php +++ b/src/DataParserPluginBase.php @@ -149,6 +149,10 @@ abstract class DataParserPluginBase extends PluginBase implements DataParserPlug * TRUE if a valid source URL was opened */ protected function nextSource() { + if (empty($this->urls)) { + return FALSE; + } + while ($this->activeUrl === NULL || (count($this->urls) - 1) > $this->activeUrl) { if (is_null($this->activeUrl)) { $this->activeUrl = 0; diff --git a/tests/src/Unit/DataParserPluginBaseTest.php b/tests/src/Unit/DataParserPluginBaseTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5e32deee36b0b4facd17de6bf98640ee85bb0eaa --- /dev/null +++ b/tests/src/Unit/DataParserPluginBaseTest.php @@ -0,0 +1,97 @@ +<?php + +namespace Drupal\Tests\migrate_plus\Unit; + +use Drupal\migrate_plus\DataParserPluginBase; +use Drupal\Tests\migrate\Unit\MigrateTestCase; + +/** + * @coversDefaultClass \Drupal\migrate_plus\DataParserPluginBase + * + * @group migrate_plus + */ +class DataParserPluginBaseTest extends MigrateTestCase { + + /** + * @covers ::nextSource + */ + public function testNextSourceWithOneUrl() { + $parser = $this->getMockedDataParser(); + $parser->expects($this->once()) + ->method('openSourceUrl') + ->willReturn(TRUE); + $this->assertTrue($parser->nextSource()); + } + + /** + * @covers ::nextSource + */ + public function testNextSourceWithoutUrls() { + $config = [ + 'urls' => [], + ]; + + $parser = $this->getMockedDataParser($config); + $parser->expects($this->never()) + ->method('openSourceUrl'); + $this->assertFalse($parser->nextSource()); + } + + /** + * @covers ::count + */ + public function testCountWithoutUrls() { + $config = [ + 'urls' => [], + ]; + + $parser = $this->getMockedDataParser($config); + $parser->expects($this->never()) + ->method('openSourceUrl'); + $this->assertEquals(0, $parser->count()); + } + + /** + * Returns a mocked data parser. + * + * @param array $configuration + * The configuration to pass to the data parser. + * + * @return \PHPUnit\Framework\MockObject\MockObject|\Drupal\Tests\migrate_plus\Unit\DataParserPluginBaseMock + * An mock instance of DataParserPluginBase. + */ + protected function getMockedDataParser(array $configuration = []) { + // Set constructor arguments. + $configuration += [ + 'urls' => ['http://example.org/data_parser_test'], + 'item_selector' => 0, + ]; + $plugin_id = 'foo'; + $plugin_definition = [ + 'id' => 'foo', + 'title' => 'Foo', + ]; + + return $this->getMockBuilder(DataParserPluginBaseMock::class) + ->setConstructorArgs([$configuration, $plugin_id, $plugin_definition]) + ->setMethods(['openSourceUrl']) + ->getMockForAbstractClass(); + } + +} + +/** + * Mock for abstract class DataParserPluginBase. + * + * This mock is used to make certain methods publicly accessible. + */ +abstract class DataParserPluginBaseMock extends DataParserPluginBase { + + /** + * {@inheritdoc} + */ + public function nextSource() { + return parent::nextSource(); + } + +}