function RelationshipUserFileDataTest::testViewsHandlerRelationshipFileToUser

Same name and namespace in other branches
  1. 11.x core/modules/file/tests/src/Kernel/Views/RelationshipUserFileDataTest.php \Drupal\Tests\file\Kernel\Views\RelationshipUserFileDataTest::testViewsHandlerRelationshipFileToUser()

Tests using the views file_to_user relationship.

File

core/modules/file/tests/src/Kernel/Views/RelationshipUserFileDataTest.php, line 175

Class

RelationshipUserFileDataTest
Tests file on user relationship handler.

Namespace

Drupal\Tests\file\Kernel\Views

Code

public function testViewsHandlerRelationshipFileToUser() : void {
  $file1 = File::create([
    'filename' => 'image-test.jpg',
    'uri' => "public://image-test.jpg",
    'filemime' => 'image/jpeg',
    'created' => 1,
    'changed' => 1,
    'status' => FileInterface::STATUS_PERMANENT,
  ]);
  $file1->enforceIsNew();
  file_put_contents($file1->getFileUri(), file_get_contents('core/tests/fixtures/files/image-1.png'));
  $file1->save();
  $file2 = File::create([
    'filename' => 'image-test-2.jpg',
    'uri' => "public://image-test-2.jpg",
    'filemime' => 'image/jpeg',
    'created' => 1,
    'changed' => 1,
    'status' => FileInterface::STATUS_PERMANENT,
  ]);
  $file2->enforceIsNew();
  file_put_contents($file2->getFileUri(), file_get_contents('core/tests/fixtures/files/image-1.png'));
  $file2->save();
  User::create([
    'name' => $this->randomMachineName(8),
    'mail' => $this->randomMachineName(4) . '@' . $this->randomMachineName(4) . '.com',
  ])
    ->save();
  $account = User::create([
    'name' => $this->randomMachineName(8),
    'mail' => $this->randomMachineName(4) . '@' . $this->randomMachineName(4) . '.com',
    'user_file' => [
      'target_id' => $file2->id(),
    ],
  ]);
  $account->save();
  $view = Views::getView('test_file_to_user');
  $this->executeView($view);
  // We should only see a single file, the one on the user account. The other
  // account's UUID, nor the other unlinked file, should appear in the
  // results.
  $expected_result = [
    [
      'fid' => $file2->id(),
      'uuid' => $account->uuid(),
    ],
  ];
  $column_map = [
    'fid' => 'fid',
    'uuid' => 'uuid',
  ];
  $this->assertIdenticalResultset($view, $expected_result, $column_map);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.