Commit a764f383 authored by quietone's avatar quietone Committed by Damien McKenna
Browse files

Issue #3120947 by DamienMcKenna, quietone: Expand FieldInstanceTest classes to...

Issue #3120947 by DamienMcKenna, quietone: Expand FieldInstanceTest classes to handle multiple bundles per entity type.
parent acd07c71
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -49,6 +49,8 @@ By DamienMcKenna: Minor code readability tweak.
#3120331 by thalles: Add directive docs into support section of composer file.
#3121289 by Dave Reid, DamienMcKenna: Normalize token types using the Token
  module mapper service.
#3120947 by DamienMcKenna, quietone: Expand FieldInstanceTest classes to handle
  multiple bundles per entity type.


Metatag 8.x-1.11, 2019-12-20
+118 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\metatag\Kernel\Plugin\migrate\source\d6;

use Drupal\Node\Entity\NodeType;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;

/**
 * Tests Metatag-D6 field instance source plugin.
 *
 * Make sure that the migration system converts Nodewords' "type" value into a
 * string that Metatag can work with.
 *
 * @covers \Drupal\metatag\Plugin\migrate\source\d6\NodewordsFieldInstance
 *
 * @group metatag
 */
class NodewordsFieldInstanceTest extends MigrateSqlSourceTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    // Core modules.
    'migrate_drupal',
    'node',
    'system',
    'taxonomy',
    'text',
    'user',

    // Contrib modules.
    'token',

    // This module.
    'metatag',
  ];

  public function setUp() {
    parent::setUp();
    $this->installEntitySchema('node');
    $this->installEntitySchema('taxonomy_term');
    $this->installEntitySchema('user');
    $this->installConfig($this->modules);

    // Create node types.
    $node_types = [
      'first_content_type' => 'first_content_type',
      'second_content_type' => 'second_content_type',
    ];
    foreach ($node_types as $node_type) {
      NodeType::create([
        'type' => $node_type,
        'name' => $node_type,
      ])->save();
    }

    // Setup vocabulary.
    Vocabulary::create([
      'vid' => 'test_vocabulary',
      'name' => 'test_vocabulary',
    ])->save();

    // Create a term.
    Term::create([
      'vid' => 'test_vocabulary',
      'name' => 'term',
    ])->save();
  }

  /**
   * {@inheritdoc}
   */
  public function providerSource() {
    $tests[0]['source_data']['nodewords'] = [
      [
        'type' => 5,
      ],
      [
        'type' => 6,
      ],
      [
        'type' => 8,
      ],
    ];

    $tests[0]['expected_data'] = [
      [
        'entity_type' => 'node',
        'type' => 5,
        'bundle' => 'first_content_type',
      ],
      [
        'entity_type' => 'node',
        'type' => 5,
        'bundle' => 'second_content_type',
      ],
      [
        'entity_type' => 'taxonomy_term',
        'type' => 6,
        'bundle' => 'test_vocabulary',
      ],
      [
        'entity_type' => 'user',
        'type' => 8,
        'bundle' => 'user',
      ],
    ];

    // The source query has 3 rows, so hardcode this value so the test passes.
    // @todo This feels like cheating?
    $tests[0]['expected_count'] = 3;
    return $tests;
  }

}
+113 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\metatag\Kernel\Plugin\migrate\source\d7;

use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;

/**
 * Tests Metatag-D7 field instance source plugin.
 *
 * @covers \Drupal\metatag\Plugin\migrate\source\d7\MetatagFieldInstance
 *
 * @group metatag
 */
class MetatagFieldInstanceTest extends MigrateSqlSourceTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
    // Core modules.
    'migrate_drupal',
    'node',
    'system',
    'taxonomy',
    'text',
    'user',

    // Contrib modules.
    'token',

    // This module.
    'metatag',
  ];

  public function setUp() {
    parent::setUp();
    $this->installEntitySchema('node');
    $this->installEntitySchema('taxonomy_term');
    $this->installEntitySchema('user');
    $this->installConfig($this->modules);

    $node_types = [
      'first_content_type' => 'first_content_type',
      'second_content_type' => 'second_content_type',
    ];
    foreach ($node_types as $node_type) {
      $node_type = NodeType::create([
        'type' => $node_type,
        'name' => $node_type,
      ]);
      $node_type->save();
    }
    //    ['taxonomy_term', ['test_vocabulary' => 'test_vocabulary']],
    //    Vocabulary::create(['name' => 'test_vocabulary']);
    // Setup vocabulary.
    Vocabulary::create([
      'vid' => 'test_vocabulary',
      'name' => 'test_vocabulary',
    ])->save();

    // Create a term and a comment.
    $term = Term::create([
      'vid' => 'test_vocabulary',
      'name' => 'term',
    ])->save();

  }

  /**
   * {@inheritdoc}
   */
  public function providerSource() {
    $tests[0]['source_data']['metatag'] = [
      [
        'entity_type' => 'node',
      ],
      [
        'entity_type' => 'taxonomy_term',
      ],
      [
        'entity_type' => 'user',
      ],
    ];

    $tests[0]['expected_data'] = [
      [
        'entity_type' => 'node',
        'bundle' => 'first_content_type',
      ],
      [
        'entity_type' => 'node',
        'bundle' => 'second_content_type',
      ],
      [
        'entity_type' => 'taxonomy_term',
        'bundle' => 'test_vocabulary',
      ],
      [
        'entity_type' => 'user',
        'bundle' => 'user',
      ],
    ];

    // The source query has 3 rows, so hardcode this value so the test passes.
    // @todo This feels like cheating?
    $tests[0]['expected_count'] = 3;
    return $tests;
  }

}
+0 −114
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\metatag\Unit\Migrate\d7;

use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfo;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\State\StateInterface;
use Drupal\metatag\Plugin\migrate\source\d6\NodewordsFieldInstance;
use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;

/**
 * Tests Metatag-D7 field instance source plugin.
 *
 * Make sure that the migration system converts Nodewords' "type" value into a
 * string that Metatag can work with.
 *
 * @see Drupal\metatag\Plugin\migrate\source\d6\NodewordsField::initializeIterator()
 *
 * @group metatag
 */
class NodewordsFieldInstanceTest extends MigrateSqlSourceTestCase {

  const PLUGIN_CLASS = NodewordsFieldInstance::class;

  protected $migrationConfiguration = [
    'id' => 'test',
    'source' => [
      'plugin' => 'd6_nodewords_field_instance',
    ],
  ];

  protected $databaseContents =
    ['nodewords' => [
      [
        'type' => 5,
        'bundle' => 'article',
      ],
      [
        'type' => 6,
        'bundle' => 'tags',
      ],
      [
        'type' => 8,
        'bundle' => 'user',
      ],
    ],
  ];

  protected $expectedResults = [
    [
      'entity_type' => 'node',
      'type' => 5,
      'bundle' => 'test_content_type',
    ],
    [
      'entity_type' => 'taxonomy_term',
      'type' => 6,
      'bundle' => 'test_vocabulary',
    ],
    [
      'entity_type' => 'user',
      'type' => 8,
      'bundle' => 'user',
    ],
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $module_handler = $this->getMock(ModuleHandlerInterface::class);
    $state = $this->getMock(StateInterface::class);
    $entity_manager = $this->getMock(EntityManagerInterface::class);
    $entity_type_bundle_info = $this->getMockBuilder(EntityTypeBundleInfo::class)
      ->disableOriginalConstructor()
      ->getMock();
    $entity_type_bundle_info->expects($this->any())
      ->method('getBundleInfo')
      ->willReturnMap([
        ['node', ['test_content_type' => 'test_content_type']],
        ['taxonomy_term', ['test_vocabulary' => 'test_vocabulary']],
        ['user', ['user' => 'user']],
      ]);

    $migration = $this->getMigration();
    // @todo Replace this.
    // $migration->expects($this->any())
    // ->method('getHighWater')
    // ->will($this->returnValue(static::ORIGINAL_HIGH_WATER));
    // Setup the plugin.
    $plugin_class = static::PLUGIN_CLASS;
    $plugin = new $plugin_class($this->migrationConfiguration['source'], $this->migrationConfiguration['source']['plugin'], [], $migration, $state, $entity_manager, $entity_type_bundle_info);

    // Do some reflection to set the database and moduleHandler.
    $plugin_reflection = new \ReflectionClass($plugin);
    $database_property = $plugin_reflection->getProperty('database');
    $database_property->setAccessible(TRUE);
    $module_handler_property = $plugin_reflection->getProperty('moduleHandler');
    $module_handler_property->setAccessible(TRUE);

    // Set the database and the module handler onto our plugin.
    $database_property->setValue($plugin, $this->getDatabase($this->databaseContents + ['test_map' => []]));
    $module_handler_property->setValue($plugin, $module_handler);

    $plugin->setStringTranslation($this->getStringTranslationStub());
    $migration->expects($this->any())
      ->method('getSourcePlugin')
      ->will($this->returnValue($plugin));
    $this->source = $plugin;
    $this->expectedCount = count($this->expectedResults);
  }

}
+0 −86
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\metatag\Unit\Migrate\d7;

use Drupal\Tests\migrate\Unit\MigrateSqlSourceTestCase;

/**
 * Tests Metatag-D7 field instance source plugin.
 *
 * @group metatag
 */
class MetatagFieldInstanceTest extends MigrateSqlSourceTestCase {

  const PLUGIN_CLASS = 'Drupal\metatag\Plugin\migrate\source\d7\MetatagFieldInstance';

  protected $migrationConfiguration = [
    'id' => 'test',
    'source' => [
      'plugin' => 'd7_metatag_field_instance',
    ],
  ];

  protected $expectedResults = [
    [
      'entity_type' => 'node',
      'bundle' => 'test_content_type',
    ],
    [
      'entity_type' => 'taxonomy_term',
      'bundle' => 'test_vocabulary',
    ],
    [
      'entity_type' => 'user',
      'bundle' => 'user',
    ],
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    $this->databaseContents['metatag'] = $this->expectedResults;

    $module_handler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
    $state = $this->getMock('Drupal\Core\State\StateInterface');
    $entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
    $entity_type_bundle_info = $this->getMockBuilder('Drupal\Core\Entity\EntityTypeBundleInfo')
      ->disableOriginalConstructor()
      ->getMock();
    $entity_type_bundle_info->expects($this->any())
      ->method('getBundleInfo')
      ->willReturnMap([
        ['node', ['test_content_type' => 'test_content_type']],
        ['taxonomy_term', ['test_vocabulary' => 'test_vocabulary']],
        ['user', ['user' => 'user']],
      ]);

    $migration = $this->getMigration();
    // @todo Replace this.
    // $migration->expects($this->any())
    // ->method('getHighWater')
    // ->will($this->returnValue(static::ORIGINAL_HIGH_WATER));
    // Setup the plugin.
    $plugin_class = static::PLUGIN_CLASS;
    $plugin = new $plugin_class($this->migrationConfiguration['source'], $this->migrationConfiguration['source']['plugin'], [], $migration, $state, $entity_manager, $entity_type_bundle_info);

    // Do some reflection to set the database and moduleHandler.
    $plugin_reflection = new \ReflectionClass($plugin);
    $database_property = $plugin_reflection->getProperty('database');
    $database_property->setAccessible(TRUE);
    $module_handler_property = $plugin_reflection->getProperty('moduleHandler');
    $module_handler_property->setAccessible(TRUE);

    // Set the database and the module handler onto our plugin.
    $database_property->setValue($plugin, $this->getDatabase($this->databaseContents + ['test_map' => []]));
    $module_handler_property->setValue($plugin, $module_handler);

    $plugin->setStringTranslation($this->getStringTranslationStub());
    $migration->expects($this->any())
      ->method('getSourcePlugin')
      ->will($this->returnValue($plugin));
    $this->source = $plugin;
    $this->expectedCount = count($this->expectedResults);
  }

}