Commit fd3ab2f5 authored by Fran Garcia-Linares's avatar Fran Garcia-Linares Committed by Chris Wells
Browse files

Issue #3310898 by fjgarlin, tim.plunkett, chrisfromredfin: Improve project fixture generation

parent dae34027
Loading
Loading
Loading
Loading
+1 −1
Changes for drush.services.yml: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -3,4 +3,4 @@ services:
    class: Drupal\project_browser\Commands\UpdateFixtureCommands
    tags:
      - { name: drush.command }
    arguments: ['@logger.factory', '@project_browser.enabled_source', '@event_dispatcher']
    arguments: ['@logger.factory', '@project_browser.enabled_source', '@event_dispatcher', '@project_browser.fixture_helper']
+1 −1

File changed.

Preview size limit exceeded, changes collapsed.

+0 −0

File changed.

Preview suppressed by a .gitattributes entry or the file's encoding is unsupported.

+4 −57
Changes for project_browser.install: 4 added lines, 57 removed lines.
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@
 * Contains install and update functions for Project Browser.
 */

use Drupal\Component\Serialization\Json;
use Drupal\Core\Database\Database;

/**
@@ -128,62 +127,6 @@ function project_browser_schema() {
  ];
}

/**
 * Inserts data into Project Browser module tables.
 */
function _project_browser_populate_from_fixture() {
  $connection = Database::getConnection();
  $module_path = \Drupal::service('module_handler')->getModule('project_browser')->getPath();
  $most_recent_change = 0;

  $projects = Json::decode(file_get_contents($module_path . '/fixtures/project_data.json'));
  $projects_chunk = array_chunk($projects, 1000);
  foreach ($projects_chunk as $chunk_projects) {
    // Insert fixture data to the database.
    $query = $connection->insert('project_browser_projects')->fields([
      'nid',
      'title',
      'author',
      'created',
      'changed',
      'project_usage_total',
      'maintenance_status',
      'development_status',
      'status',
      'field_security_advisory_coverage',
      'flag_project_star_user_count',
      'field_project_type',
      'project_data',
      'field_project_machine_name',
    ]);
    foreach ($chunk_projects as $project) {
      if ($project['changed'] > $most_recent_change) {
        $most_recent_change = $project['changed'];
      }
      // Map from fixture format to the expected by the database.
      $project_data = unserialize($project['project_data']);
      $project['maintenance_status'] = $project_data['taxonomy_vocabulary_44']['id'];
      $project['development_status'] = $project_data['taxonomy_vocabulary_46']['id'];
      $project['field_project_machine_name'] = $project_data['field_project_machine_name'];

      $query->values($project);
    }
    $query->execute();
  }

  $categories = Json::decode(file_get_contents($module_path . '/fixtures/categories.json'));
  $category_query = $connection->insert('project_browser_categories')->fields([
    'tid',
    'pid',
  ]);
  foreach ($categories as $category) {
    $category_query->values((array) $category);
  }
  $category_query->execute();

  \Drupal::state()->set('project_browser.last_imported', $most_recent_change);
}

/**
 * Implements hook_install().
 *
@@ -269,3 +212,7 @@ function project_browser_update_9008() {
  $connection->truncate('project_browser_categories')->execute();
  _project_browser_populate_from_fixture();
}

function project_browser_update_9009() {
  _project_browser_populate_from_fixture();
}
+8 −0
Changes for project_browser.module: 8 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -30,3 +30,11 @@ function project_browser_theme() {
    ],
  ];
}

/**
 * Inserts data into Project Browser module tables.
 */
function _project_browser_populate_from_fixture($truncate_first = TRUE) {
  $module_path = \Drupal::moduleHandler()->getModule('project_browser')->getPath();
  \Drupal::service('project_browser.fixture_helper')->populateFromFixture($module_path . '/fixtures/project_data.json', $module_path . '/fixtures/categories.json', $truncate_first);
}
Loading