Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
CRAP
95.83% covered (success)
95.83%
23 / 24
Role
0.00% covered (danger)
0.00%
0 / 1
80.00% covered (warning)
80.00%
4 / 5
8
95.83% covered (success)
95.83%
23 / 24
 query
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
4 / 4
 fields
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
0 / 0
 initializeIterator
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
8 / 8
 prepareRow
0.00% covered (danger)
0.00%
0 / 1
2.00
90.00% covered (success)
90.00%
9 / 10
 getIds
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
<?php
/**
 * @file
 * Contains \Drupal\user\Plugin\migrate\source\d6\Role.
 */
namespace Drupal\user\Plugin\migrate\source\d6;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
/**
 * Drupal 6 role source from database.
 *
 * @MigrateSource(
 *   id = "d6_user_role"
 * )
 */
class Role extends DrupalSqlBase {
  /**
   * List of filter IDs per role IDs.
   *
   * @var array
   */
  protected $filterPermissions = array();
  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this->select('role', 'r')
      ->fields('r', array('rid', 'name'))
      ->orderBy('rid');
    return $query;
  }
  /**
   * {@inheritdoc}
   */
  public function fields() {
    return array(
      'rid' => $this->t('Role ID.'),
      'name' => $this->t('The name of the user role.'),
    );
  }
  /**
   * {@inheritdoc}
   */
  protected function initializeIterator() {
    $filter_roles = $this->select('filter_formats', 'f')
      ->fields('f', array('format', 'roles'))
      ->execute()
      ->fetchAllKeyed();
    foreach ($filter_roles as $format => $roles) {
      // Drupal 6 code: $roles = ','. implode(',', $roles) .',';
      // Remove the beginning and ending comma.
      foreach (explode(',', trim($roles, ',')) as $rid) {
        $this->filterPermissions[$rid][] = $format;
      }
    }
    return parent::initializeIterator();
  }
  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    $rid = $row->getSourceProperty('rid');
    $permissions = $this->select('permission', 'p')
      ->fields('p', array('perm'))
      ->condition('rid', $rid)
      ->execute()
      ->fetchField();
    $row->setSourceProperty('permissions', explode(', ', $permissions));
    if (isset($this->filterPermissions[$rid])) {
      $row->setSourceProperty("filter_permissions:$rid", $this->filterPermissions[$rid]);
    }
    return parent::prepareRow($row);
  }
  /**
   * {@inheritdoc}
   */
  public function getIds() {
    $ids['rid']['type'] = 'integer';
    return $ids;
  }
}