You are here

public function ProfileViewTest::testProfileRelationship in Profile 8

Tests views relationship with multiple referenced entities.

Relationship is required, so only users with profiles will be listed.

File

tests/src/Kernel/ProfileViewTest.php, line 49

Class

ProfileViewTest
Tests profile integration with Views.

Namespace

Drupal\Tests\profile\Kernel

Code

public function testProfileRelationship() {
  $profile_type = $this
    ->createProfileType();
  $user[0] = $user1 = User::create([
    'name' => mb_strtolower($this
      ->randomMachineName()),
    'status' => TRUE,
  ]);
  $user1
    ->save();
  $user2 = User::create([
    'name' => mb_strtolower($this
      ->randomMachineName()),
    'status' => TRUE,
  ]);
  $user2
    ->save();
  $user[1] = $user3 = User::create([
    'name' => mb_strtolower($this
      ->randomMachineName()),
    'status' => TRUE,
  ]);
  $user3
    ->save();
  $user4 = User::create([
    'name' => mb_strtolower($this
      ->randomMachineName()),
    'status' => TRUE,
  ]);
  $user4
    ->save();
  $profile[0] = $this
    ->createProfile($profile_type, $user1);
  $profile[1] = $this
    ->createProfile($profile_type, $user3);
  Views::viewsData()
    ->clear();

  // Check table relationship exists.
  $views_data = Views::viewsData()
    ->get('users_field_data');
  $this
    ->assertEquals($views_data['profile']['relationship']['base'], 'profile');
  $this
    ->assertEquals($views_data['profile']['relationship']['base field'], 'uid');
  $view = Views::getView('users');
  $this
    ->executeView($view);

  // Ensure values are populated for user and profiles.
  foreach ($view->result as $index => $row) {
    $this
      ->assertEquals($row->uid, $user[$index]
      ->id(), 'User ' . $user[$index]
      ->id() . ' found on row: ' . $index);
    $this
      ->assertEquals($row->profile_users_field_data_profile_id, $profile[$index]
      ->id(), 'Profile ' . $profile[$index]
      ->id() . ' found on view: ' . $index);
  }
}