You are here

class MigrationPluginsAltererTest in Paragraphs 8

Tests the MigrationPluginsAlterer service.

@todo Cover every method.

@coversDefaultClass \Drupal\paragraphs\MigrationPluginsAlterer

@group paragraphs

Hierarchy

Expanded class hierarchy of MigrationPluginsAltererTest

File

tests/src/Unit/migrate/MigrationPluginsAltererTest.php, line 17

Namespace

Drupal\Tests\paragraphs\Unit\migrate
View source
class MigrationPluginsAltererTest extends UnitTestCase {

  /**
   * The migration plugin alterer.
   *
   * @var \Drupal\paragraphs\MigrationPluginsAlterer
   */
  protected $paragraphsMigrationPluginsAlterer;

  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $logger_channel = $this
      ->createMock('Drupal\\Core\\Logger\\LoggerChannelInterface');
    $logger_factory = $this
      ->getMockBuilder('Drupal\\Core\\Logger\\LoggerChannelFactory')
      ->getMock();
    $logger_factory
      ->expects($this
      ->atLeastOnce())
      ->method('get')
      ->with('paragraphs')
      ->will($this
      ->returnValue($logger_channel));
    $this->paragraphsMigrationPluginsAlterer = new MigrationPluginsAlterer($logger_factory);
  }

  /**
   * Tests that migration processes are transformed to an array of processors.
   *
   * @dataProvider providerParagraphsMigrationPrepareProcess
   * @covers ::paragraphsMigrationPrepareProcess
   */
  public function testParagraphsMigrationPrepareProcess(array $input, array $expected) {
    [
      'process' => $process,
      'property' => $property,
    ] = $input;
    $success = $this->paragraphsMigrationPluginsAlterer
      ->paragraphsMigrationPrepareProcess($process, $property);
    $this
      ->assertSame($expected['return'], $success);
    $this
      ->assertEquals($expected['process'], $process);
  }

  /**
   * Provides data and expected results for testing the prepare process method.
   *
   * @return array[]
   *   Data and expected results.
   */
  public function providerParagraphsMigrationPrepareProcess() {
    return [
      // Missing property (no change).
      [
        'input' => [
          'process' => [
            'catname' => 'Picurka',
            'wont/touch' => 'this',
          ],
          'property' => 'missing',
        ],
        'expected' => [
          'return' => FALSE,
          'process' => [
            'catname' => 'Picurka',
            'wont/touch' => 'this',
          ],
        ],
      ],
      // Existing string property.
      [
        'input' => [
          'process' => [
            'catname' => 'Picurka',
            'wont/touch' => 'this',
          ],
          'property' => 'catname',
        ],
        'expected' => [
          'return' => TRUE,
          'process' => [
            'catname' => [
              [
                'plugin' => 'get',
                'source' => 'Picurka',
              ],
            ],
            'wont/touch' => 'this',
          ],
        ],
      ],
      // Single process plugin.
      [
        'input' => [
          'process' => [
            'cat' => [
              'plugin' => 'migration_lookup',
              'migration' => 'cats',
              'source' => 'cat_id',
            ],
          ],
          'property' => 'cat',
        ],
        'expected' => [
          'return' => TRUE,
          'process' => [
            'cat' => [
              [
                'plugin' => 'migration_lookup',
                'migration' => 'cats',
                'source' => 'cat_id',
              ],
            ],
          ],
        ],
      ],
      // Array of process plugins (no change).
      [
        'input' => [
          'process' => [
            'catname' => [
              [
                'plugin' => 'migration_lookup',
                'migration' => 'cats',
                'source' => 'cat_id',
              ],
              [
                'plugin' => 'extract',
                'index' => [
                  'name',
                ],
              ],
              [
                'plugin' => 'callback',
                'callable' => 'ucfirst',
              ],
            ],
          ],
          'property' => 'catname',
        ],
        'expected' => [
          'return' => TRUE,
          'process' => [
            'catname' => [
              [
                'plugin' => 'migration_lookup',
                'migration' => 'cats',
                'source' => 'cat_id',
              ],
              [
                'plugin' => 'extract',
                'index' => [
                  'name',
                ],
              ],
              [
                'plugin' => 'callback',
                'callable' => 'ucfirst',
              ],
            ],
          ],
        ],
      ],
      // Invalid type.
      [
        'input' => [
          'process' => [
            'invalid' => (object) [
              [
                'not a' => 'kitten',
              ],
            ],
          ],
          'property' => 'invalid',
        ],
        'expected' => [
          'return' => FALSE,
          'process' => [
            'invalid' => (object) [
              [
                'not a' => 'kitten',
              ],
            ],
          ],
        ],
      ],
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrationPluginsAltererTest::$paragraphsMigrationPluginsAlterer protected property The migration plugin alterer.
MigrationPluginsAltererTest::providerParagraphsMigrationPrepareProcess public function Provides data and expected results for testing the prepare process method.
MigrationPluginsAltererTest::setUp protected function Overrides UnitTestCase::setUp
MigrationPluginsAltererTest::testParagraphsMigrationPrepareProcess public function Tests that migration processes are transformed to an array of processors.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.