Version 1
[yaffs-website] / web / core / modules / file / src / Tests / Views / RelationshipUserFileDataTest.php
1 <?php
2
3 namespace Drupal\file\Tests\Views;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\file\Entity\File;
7 use Drupal\views\Tests\ViewTestBase;
8 use Drupal\views\Views;
9 use Drupal\views\Tests\ViewTestData;
10 use Drupal\field\Entity\FieldStorageConfig;
11
12 /**
13  * Tests file on user relationship handler.
14  *
15  * @group file
16  */
17 class RelationshipUserFileDataTest extends ViewTestBase {
18
19   /**
20    * Modules to install.
21    *
22    * @var array
23    */
24   public static $modules = ['file', 'file_test_views', 'user'];
25
26   /**
27    * Views used by this test.
28    *
29    * @var array
30    */
31   public static $testViews = ['test_file_user_file_data'];
32
33   protected function setUp() {
34     parent::setUp();
35
36     // Create the user profile field and instance.
37     FieldStorageConfig::create([
38       'entity_type' => 'user',
39       'field_name' => 'user_file',
40       'type' => 'file',
41       'translatable' => '0',
42     ])->save();
43     FieldConfig::create([
44       'label' => 'User File',
45       'description' => '',
46       'field_name' => 'user_file',
47       'entity_type' => 'user',
48       'bundle' => 'user',
49       'required' => 0,
50     ])->save();
51
52     ViewTestData::createTestViews(get_class($this), ['file_test_views']);
53   }
54
55   /**
56    * Tests using the views file relationship.
57    */
58   public function testViewsHandlerRelationshipUserFileData() {
59     $file = File::create([
60       'fid' => 2,
61       'uid' => 2,
62       'filename' => 'image-test.jpg',
63       'uri' => "public://image-test.jpg",
64       'filemime' => 'image/jpeg',
65       'created' => 1,
66       'changed' => 1,
67       'status' => FILE_STATUS_PERMANENT,
68     ]);
69     $file->enforceIsNew();
70     file_put_contents($file->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png'));
71     $file->save();
72
73     $account = $this->drupalCreateUser();
74     $account->user_file->target_id = 2;
75     $account->save();
76
77     $view = Views::getView('test_file_user_file_data');
78     // Tests \Drupal\taxonomy\Plugin\views\relationship\NodeTermData::calculateDependencies().
79     $expected = [
80       'module' => [
81         'file',
82         'user',
83       ],
84     ];
85     $this->assertIdentical($expected, $view->getDependencies());
86     $this->executeView($view);
87     $expected_result = [
88       [
89         'file_managed_user__user_file_fid' => '2',
90       ],
91     ];
92     $column_map = ['file_managed_user__user_file_fid' => 'file_managed_user__user_file_fid'];
93     $this->assertIdenticalResultset($view, $expected_result, $column_map);
94   }
95
96 }