diff --git a/core/modules/migrate_drupal/tests/fixtures/drupal6.php b/core/modules/migrate_drupal/tests/fixtures/drupal6.php index 1092c11..e80488c 100644 --- a/core/modules/migrate_drupal/tests/fixtures/drupal6.php +++ b/core/modules/migrate_drupal/tests/fixtures/drupal6.php @@ -41614,6 +41614,45 @@ 'mysql_character_set' => 'utf8', )); +$connection->insert('node_counter') +->fields(array( + 'nid', + 'totalcount', + 'daycount', + 'timestamp', +)) +->values(array( + 'nid' => '1', + 'totalcount' => '2', + 'daycount' => '0', + 'timestamp' => '1421727536', +)) +->values(array( + 'nid' => '2', + 'totalcount' => '1', + 'daycount' => '0', + 'timestamp' => '1471428059', +)) +->values(array( + 'nid' => '3', + 'totalcount' => '1', + 'daycount' => '0', + 'timestamp' => '1471428153', +)) +->values(array( + 'nid' => '4', + 'totalcount' => '1', + 'daycount' => '1', + 'timestamp' => '1478755275', +)) +->values(array( + 'nid' => '5', + 'totalcount' => '1', + 'daycount' => '1', + 'timestamp' => '1478755314', +)) +->execute(); + $connection->schema()->createTable('node_revisions', array( 'fields' => array( 'nid' => array( diff --git a/core/modules/statistics/migration_templates/d6_statistics_node_counter.yml b/core/modules/statistics/migration_templates/d6_statistics_node_counter.yml new file mode 100644 index 0000000..0f9ecb9 --- /dev/null +++ b/core/modules/statistics/migration_templates/d6_statistics_node_counter.yml @@ -0,0 +1,23 @@ +id: d6_statistics_node_counter +label: Drupal 6 Node Counter +migration_tags: + - Drupal 6 +source: + plugin: node_counter +process: + nid: + - + plugin: migration + migration: d6_node + source: nid + - + plugin: skip_on_empty + method: row + totalcount: totalcount + daycount: daycount + timestamp: timestamp +destination: + plugin: node_counter +migration_dependencies: + required: + - d6_node diff --git a/core/modules/statistics/migration_templates/d7_statistics_node_counter.yml b/core/modules/statistics/migration_templates/d7_statistics_node_counter.yml new file mode 100644 index 0000000..afaf7f3 --- /dev/null +++ b/core/modules/statistics/migration_templates/d7_statistics_node_counter.yml @@ -0,0 +1,23 @@ +id: d7_statistics_node_counter +label: Drupal 7 Node Counter +migration_tags: + - Drupal 7 +source: + plugin: node_counter +process: + nid: + - + plugin: migration + migration: d7_node + source: nid + - + plugin: skip_on_empty + method: row + totalcount: totalcount + daycount: daycount + timestamp: timestamp +destination: + plugin: node_counter +migration_dependencies: + required: + - d7_node diff --git a/core/modules/statistics/src/Plugin/migrate/destination/NodeCounter.php b/core/modules/statistics/src/Plugin/migrate/destination/NodeCounter.php new file mode 100644 index 0000000..1d8c987 --- /dev/null +++ b/core/modules/statistics/src/Plugin/migrate/destination/NodeCounter.php @@ -0,0 +1,99 @@ +connection = $connection; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition, MigrationInterface $migration = NULL) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $migration, + $container->get('database') + ); + } + + /** + * {@inheritdoc} + */ + public function getIds() { + return ['nid' => ['type' => 'integer']]; + } + + /** + * {@inheritdoc} + */ + public function fields(MigrationInterface $migration = NULL) { + return [ + 'nid' => $this->t('The ID of the node to which these statistics apply.'), + 'totalcount' => $this->t('The total number of times the node has been viewed.'), + 'daycount' => $this->t('The total number of times the node has been viewed today.'), + 'timestamp' => $this->t('The most recent time the node has been viewed.'), + ]; + } + + /** + * {@inheritdoc} + */ + public function import(Row $row, array $old_destination_id_values = array()) { + return (bool) $this->connection + ->insert('node_counter') + ->fields([ + 'nid', + 'daycount', + 'totalcount', + 'timestamp', + ], [ + $row->getDestinationProperty('nid'), + $row->getDestinationProperty('daycount'), + $row->getDestinationProperty('totalcount'), + $row->getDestinationProperty('timestamp'), + ]) + ->execute(); + } + +} diff --git a/core/modules/statistics/src/Plugin/migrate/source/NodeCounter.php b/core/modules/statistics/src/Plugin/migrate/source/NodeCounter.php new file mode 100644 index 0000000..2ac7535 --- /dev/null +++ b/core/modules/statistics/src/Plugin/migrate/source/NodeCounter.php @@ -0,0 +1,43 @@ +select('node_counter', 'nc')->fields('nc'); + } + + /** + * {@inheritdoc} + */ + public function fields() { + return [ + 'nid' => $this->t('The node ID.'), + 'totalcount' => $this->t('The total number of times the node has been viewed.'), + 'daycount' => $this->t('The total number of times the node has been viewed today.'), + 'timestamp' => $this->t('The most recent time the node has been viewed.'), + ]; + } + + /** + * {@inheritdoc} + */ + public function getIds() { + $ids['nid']['type'] = 'integer'; + return $ids; + } + +} diff --git a/core/modules/statistics/tests/src/Kernel/Migrate/d6/MigrateNodeCounterTest.php b/core/modules/statistics/tests/src/Kernel/Migrate/d6/MigrateNodeCounterTest.php new file mode 100644 index 0000000..a8ae433c --- /dev/null +++ b/core/modules/statistics/tests/src/Kernel/Migrate/d6/MigrateNodeCounterTest.php @@ -0,0 +1,76 @@ +installEntitySchema('node'); + $this->installConfig('node'); + $this->installSchema('statistics', ['node_counter']); + + $this->executeMigrations([ + 'd6_filter_format', + 'd6_user_role', + 'd6_node_settings', + 'd6_user', + 'd6_node_type', + 'd6_node', + 'd6_statistics_node_counter' + ]); + } + + /** + * Tests migration of node counter. + */ + public function testStatisticsSettings() { + $this->assertNodeCounter(1, 2, 0, 1421727536); + $this->assertNodeCounter(2, 1, 0, 1471428059); + $this->assertNodeCounter(3, 1, 0, 1471428153); + $this->assertNodeCounter(4, 1, 1, 1478755275); + $this->assertNodeCounter(5, 1, 1, 1478755314); + } + + /** + * Asserts various aspects of a node counter. + * + * @param integer $nid + * The node ID. + * @param integer $total_count + * The expected total count. + * @param integer $day_count + * The expected day count. + * @param integer $timestamp + * The expected timestamp. + */ + protected function assertNodeCounter($nid, $total_count, $day_count, $timestamp) { + /** @var StatisticsViewsResult $statistics */ + $statistics = $this->container->get('statistics.storage.node')->fetchView($nid); + $this->assertEquals($total_count, $statistics->getTotalCount()); + $this->assertEquals($day_count, $statistics->getDayCount()); + $this->assertEquals($timestamp, $statistics->getTimestamp()); + } + +} diff --git a/core/modules/statistics/tests/src/Kernel/Migrate/d7/MigrateNodeCounterTest.php b/core/modules/statistics/tests/src/Kernel/Migrate/d7/MigrateNodeCounterTest.php new file mode 100644 index 0000000..f4a4f99 --- /dev/null +++ b/core/modules/statistics/tests/src/Kernel/Migrate/d7/MigrateNodeCounterTest.php @@ -0,0 +1,72 @@ +installEntitySchema('node'); + $this->installConfig('node'); + $this->installSchema('statistics', ['node_counter']); + + $this->executeMigrations([ + 'd7_user_role', + 'd7_user', + 'd7_node_type', + 'd7_node', + 'd7_statistics_node_counter' + ]); + } + + /** + * Tests migration of node counter. + */ + public function testStatisticsSettings() { + $this->assertNodeCounter(1, 2, 0, 1421727536); + $this->assertNodeCounter(2, 1, 0, 1471428059); + $this->assertNodeCounter(4, 1, 1, 1478755275); + } + + /** + * Asserts various aspects of a node counter. + * + * @param integer $nid + * The node ID. + * @param integer $total_count + * The expected total count. + * @param integer $day_count + * The expected day count. + * @param integer $timestamp + * The expected timestamp. + */ + protected function assertNodeCounter($nid, $total_count, $day_count, $timestamp) { + /** @var StatisticsViewsResult $statistics */ + $statistics = $this->container->get('statistics.storage.node')->fetchView($nid); + $this->assertEquals($total_count, $statistics->getTotalCount()); + $this->assertEquals($day_count, $statistics->getDayCount()); + $this->assertEquals($timestamp, $statistics->getTimestamp()); + } + +} diff --git a/core/modules/statistics/tests/src/Kernel/Plugin/NodeCounterTest.php b/core/modules/statistics/tests/src/Kernel/Plugin/NodeCounterTest.php new file mode 100644 index 0000000..7248c4e --- /dev/null +++ b/core/modules/statistics/tests/src/Kernel/Plugin/NodeCounterTest.php @@ -0,0 +1,66 @@ + 1, + 'totalcount' => 2, + 'daycount' => 0, + 'timestamp' => 1421727536, + ], + [ + 'nid' => 2, + 'totalcount' => 1, + 'daycount' => 0, + 'timestamp' => 1471428059, + ], + [ + 'nid' => 3, + 'totalcount' => 1, + 'daycount' => 0, + 'timestamp' => 1471428153, + ], + [ + 'nid' => 4, + 'totalcount' => 1, + 'daycount' => 1, + 'timestamp' => 1478755275, + ], + [ + 'nid' => 5, + 'totalcount' => 1, + 'daycount' => 1, + 'timestamp' => 1478755314, + ], + ]; + + // The expected results. + $tests[0]['expected_data'] = $tests[0]['source_data']['node_counter']; + + return $tests; + } + +}