diff --git a/core/modules/file/src/Tests/Views/RelationshipFileToUserTest.php b/core/modules/file/src/Tests/Views/RelationshipFileToUserTest.php deleted file mode 100644 index c3836ed43cc..00000000000 --- a/core/modules/file/src/Tests/Views/RelationshipFileToUserTest.php +++ /dev/null @@ -1,108 +0,0 @@ - 'user', - 'field_name' => 'user_file', - 'type' => 'file', - 'translatable' => '0', - ])->save(); - FieldConfig::create([ - 'label' => 'User File', - 'description' => '', - 'field_name' => 'user_file', - 'entity_type' => 'user', - 'bundle' => 'user', - 'required' => 0, - ])->save(); - - ViewTestData::createTestViews(get_class($this), ['file_test_views']); - } - - /** - * Tests using the views file relationship. - */ - public function testViewsHandlerRelationshipFileToUser() { - $file1 = File::create([ - 'fid' => 2, - 'uid' => 2, - 'filename' => 'image-test.jpg', - 'uri' => "public://image-test.jpg", - 'filemime' => 'image/jpeg', - 'created' => 1, - 'changed' => 1, - 'status' => FILE_STATUS_PERMANENT, - ]); - $file1->enforceIsNew(); - file_put_contents($file1->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png')); - $file1->save(); - - $file2 = File::create([ - 'fid' => 3, - 'uid' => 2, - 'filename' => 'image-test-2.jpg', - 'uri' => "public://image-test-2.jpg", - 'filemime' => 'image/jpeg', - 'created' => 1, - 'changed' => 1, - 'status' => FILE_STATUS_PERMANENT, - ]); - $file2->enforceIsNew(); - file_put_contents($file2->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png')); - $file2->save(); - - $this->drupalCreateUser(); - - $account = $this->drupalCreateUser(); - $account->user_file->target_id = 2; - $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' => '2', - 'uuid' => $account->uuid(), - ], - ]; - $column_map = ['fid' => 'fid', 'uuid' => 'uuid']; - $this->assertIdenticalResultset($view, $expected_result, $column_map); - } - -} diff --git a/core/modules/file/src/Tests/Views/RelationshipUserFileDataTest.php b/core/modules/file/src/Tests/Views/RelationshipUserFileDataTest.php deleted file mode 100644 index 6a5f53b5d36..00000000000 --- a/core/modules/file/src/Tests/Views/RelationshipUserFileDataTest.php +++ /dev/null @@ -1,96 +0,0 @@ - 'user', - 'field_name' => 'user_file', - 'type' => 'file', - 'translatable' => '0', - ])->save(); - FieldConfig::create([ - 'label' => 'User File', - 'description' => '', - 'field_name' => 'user_file', - 'entity_type' => 'user', - 'bundle' => 'user', - 'required' => 0, - ])->save(); - - ViewTestData::createTestViews(get_class($this), ['file_test_views']); - } - - /** - * Tests using the views file relationship. - */ - public function testViewsHandlerRelationshipUserFileData() { - $file = File::create([ - 'fid' => 2, - 'uid' => 2, - 'filename' => 'image-test.jpg', - 'uri' => "public://image-test.jpg", - 'filemime' => 'image/jpeg', - 'created' => 1, - 'changed' => 1, - 'status' => FILE_STATUS_PERMANENT, - ]); - $file->enforceIsNew(); - file_put_contents($file->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png')); - $file->save(); - - $account = $this->drupalCreateUser(); - $account->user_file->target_id = 2; - $account->save(); - - $view = Views::getView('test_file_user_file_data'); - // Tests \Drupal\taxonomy\Plugin\views\relationship\NodeTermData::calculateDependencies(). - $expected = [ - 'module' => [ - 'file', - 'user', - ], - ]; - $this->assertIdentical($expected, $view->getDependencies()); - $this->executeView($view); - $expected_result = [ - [ - 'file_managed_user__user_file_fid' => '2', - ], - ]; - $column_map = ['file_managed_user__user_file_fid' => 'file_managed_user__user_file_fid']; - $this->assertIdenticalResultset($view, $expected_result, $column_map); - } - -} diff --git a/core/modules/file/src/Tests/Views/RelationshipUserToFileTest.php b/core/modules/file/src/Tests/Views/RelationshipUserToFileTest.php deleted file mode 100644 index da7d54ca1df..00000000000 --- a/core/modules/file/src/Tests/Views/RelationshipUserToFileTest.php +++ /dev/null @@ -1,108 +0,0 @@ - 'user', - 'field_name' => 'user_file', - 'type' => 'file', - 'translatable' => '0', - ])->save(); - FieldConfig::create([ - 'label' => 'User File', - 'description' => '', - 'field_name' => 'user_file', - 'entity_type' => 'user', - 'bundle' => 'user', - 'required' => 0, - ])->save(); - - ViewTestData::createTestViews(get_class($this), ['file_test_views']); - } - - /** - * Tests using the views file relationship. - */ - public function testViewsHandlerRelationshipFileToUser() { - $file1 = File::create([ - 'fid' => 2, - 'uid' => 2, - 'filename' => 'image-test.jpg', - 'uri' => "public://image-test.jpg", - 'filemime' => 'image/jpeg', - 'created' => 1, - 'changed' => 1, - 'status' => FILE_STATUS_PERMANENT, - ]); - $file1->enforceIsNew(); - file_put_contents($file1->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png')); - $file1->save(); - - $file2 = File::create([ - 'fid' => 3, - 'uid' => 2, - 'filename' => 'image-test-2.jpg', - 'uri' => "public://image-test-2.jpg", - 'filemime' => 'image/jpeg', - 'created' => 1, - 'changed' => 1, - 'status' => FILE_STATUS_PERMANENT, - ]); - $file2->enforceIsNew(); - file_put_contents($file2->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png')); - $file2->save(); - - $this->drupalCreateUser(); - - $account = $this->drupalCreateUser(); - $account->user_file->target_id = 2; - $account->save(); - - $view = Views::getView('test_user_to_file'); - $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' => '2', - 'name' => $account->getDisplayName(), - ], - ]; - $column_map = ['fid' => 'fid', 'name' => 'name']; - $this->assertIdenticalResultset($view, $expected_result, $column_map); - } - -} diff --git a/core/modules/file/tests/modules/file_test_views/config/optional/node.type.file_test.yml b/core/modules/file/tests/modules/file_test_views/config/optional/node.type.file_test.yml new file mode 100644 index 00000000000..d5efeed2110 --- /dev/null +++ b/core/modules/file/tests/modules/file_test_views/config/optional/node.type.file_test.yml @@ -0,0 +1,9 @@ +type: file_test +name: File test +description: 'Bundle for testing file relationships.' +help: '' +new_revision: true +display_submitted: true +preview_mode: 1 +status: true +langcode: en diff --git a/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_file_to_node.yml b/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_file_to_node.yml new file mode 100644 index 00000000000..f696b99fc94 --- /dev/null +++ b/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_file_to_node.yml @@ -0,0 +1,217 @@ +langcode: en +status: true +dependencies: + module: + - file + - node + - user +id: test_file_to_node +label: test_file_to_node +module: views +description: '' +tag: '' +base_table: file_managed +base_field: fid +core: 8.x +display: + default: + display_plugin: default + id: default + display_title: Master + position: 0 + display_options: + access: + type: perm + options: + perm: 'access content' + cache: + type: tag + options: { } + query: + type: views_query + options: + disable_sql_rewrite: false + distinct: false + replica: false + query_comment: '' + query_tags: { } + exposed_form: + type: basic + options: + submit_button: Apply + reset_button: false + reset_button_label: Reset + exposed_sorts_label: 'Sort by' + expose_sort_order: true + sort_asc_label: Asc + sort_desc_label: Desc + pager: + type: mini + options: + items_per_page: 10 + offset: 0 + id: 0 + total_pages: null + expose: + items_per_page: false + items_per_page_label: 'Items per page' + items_per_page_options: '5, 10, 25, 50' + items_per_page_options_all: false + items_per_page_options_all_label: '- All -' + offset: false + offset_label: Offset + tags: + previous: ‹‹ + next: ›› + style: + type: default + options: + grouping: { } + row_class: '' + default_row_class: true + uses_fields: false + row: + type: fields + options: + inline: { } + separator: '' + hide_empty: false + default_field_elements: true + fields: + filename: + id: filename + table: file_managed + field: filename + entity_type: file + entity_field: filename + label: '' + alter: + alter_text: false + make_link: false + absolute: false + trim: false + word_boundary: false + ellipsis: false + strip_tags: false + html: false + hide_empty: false + empty_zero: false + plugin_id: field + type: file_link + relationship: none + group_type: group + admin_label: '' + exclude: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: true + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_alter_empty: true + click_sort_column: value + settings: { } + group_column: value + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false + nid: + id: nid + table: node_field_data + field: nid + relationship: file_to_node_1 + group_type: group + admin_label: '' + label: '' + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: false + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + click_sort_column: value + type: number_integer + settings: + thousand_separator: '' + prefix_suffix: true + group_column: value + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false + entity_type: node + entity_field: nid + plugin_id: field + filters: { } + sorts: { } + header: { } + footer: { } + empty: { } + relationships: + file_to_node_1: + id: file_to_node_1 + table: file_usage + field: file_to_node + relationship: none + group_type: group + admin_label: Content + required: true + plugin_id: standard + arguments: { } + display_extenders: { } + cache_metadata: + max-age: -1 + contexts: + - 'languages:language_content' + - 'languages:language_interface' + - url.query_args + - user.permissions + tags: { } diff --git a/core/modules/file/tests/modules/file_test_views/test_views/views.view.test_file_to_user.yml b/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_file_to_user.yml similarity index 98% rename from core/modules/file/tests/modules/file_test_views/test_views/views.view.test_file_to_user.yml rename to core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_file_to_user.yml index 8cf41cfa25d..f5b04371ff1 100644 --- a/core/modules/file/tests/modules/file_test_views/test_views/views.view.test_file_to_user.yml +++ b/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_file_to_user.yml @@ -198,6 +198,10 @@ display: id: file_to_user table: file_usage field: file_to_user + relationship: none + group_type: group + admin_label: User + required: true arguments: { } display_extenders: { } cache_metadata: diff --git a/core/modules/file/tests/modules/file_test_views/test_views/views.view.test_file_user_file_data.yml b/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_file_user_file_data.yml similarity index 100% rename from core/modules/file/tests/modules/file_test_views/test_views/views.view.test_file_user_file_data.yml rename to core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_file_user_file_data.yml diff --git a/core/modules/file/tests/modules/file_test_views/test_views/views.view.test_user_to_file.yml b/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_node_to_file.yml similarity index 87% rename from core/modules/file/tests/modules/file_test_views/test_views/views.view.test_user_to_file.yml rename to core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_node_to_file.yml index b43ce12f4a9..a4c4e9ae718 100644 --- a/core/modules/file/tests/modules/file_test_views/test_views/views.view.test_user_to_file.yml +++ b/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_node_to_file.yml @@ -3,14 +3,15 @@ status: true dependencies: module: - file + - node - user -id: test_user_to_file -label: test_user_to_file +id: test_node_to_file +label: test_node_to_file module: views description: '' tag: '' -base_table: users_field_data -base_field: uid +base_table: node_field_data +base_field: nid core: 8.x display: default: @@ -22,7 +23,7 @@ display: access: type: perm options: - perm: 'access user profiles' + perm: 'access content' cache: type: tag options: { } @@ -77,12 +78,12 @@ display: hide_empty: false default_field_elements: true fields: - name: - id: name - table: users_field_data - field: name - entity_type: user - entity_field: name + title: + id: title + table: node_field_data + field: title + entity_type: node + entity_field: title label: '' alter: alter_text: false @@ -95,6 +96,8 @@ display: html: false hide_empty: false empty_zero: false + settings: + link_to_entity: true plugin_id: field relationship: none group_type: group @@ -111,8 +114,7 @@ display: empty: '' hide_alter_empty: true click_sort_column: value - type: user_name - settings: { } + type: string group_column: value group_columns: { } group_rows: true @@ -127,7 +129,7 @@ display: id: fid table: file_managed field: fid - relationship: user_to_file + relationship: node_to_file group_type: group admin_label: '' label: '' @@ -189,27 +191,16 @@ display: entity_type: file entity_field: fid plugin_id: field - filters: - status: - value: '1' - table: users_field_data - field: status - plugin_id: boolean - entity_type: user - entity_field: status - id: status - expose: - operator: '' - group: 1 + filters: { } sorts: { } header: { } footer: { } empty: { } relationships: - user_to_file: - id: user_to_file + node_to_file: + id: node_to_file table: file_usage - field: user_to_file + field: node_to_file relationship: none group_type: group admin_label: File @@ -223,5 +214,6 @@ display: - 'languages:language_content' - 'languages:language_interface' - url.query_args + - 'user.node_grants:view' - user.permissions tags: { } diff --git a/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_user_to_file.yml b/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_user_to_file.yml new file mode 100644 index 00000000000..e6323de5878 --- /dev/null +++ b/core/modules/file/tests/modules/file_test_views/config/optional/views.view.test_user_to_file.yml @@ -0,0 +1,233 @@ +langcode: en +status: true +dependencies: + module: + - file + - user +id: test_user_to_file +label: test_user_to_file +module: views +description: '' +tag: '' +base_table: users_field_data +base_field: uid +core: 8.x +display: + default: + display_plugin: default + id: default + display_title: Master + position: 0 + display_options: + access: + type: perm + options: + perm: 'access user profiles' + cache: + type: tag + options: { } + query: + type: views_query + options: + disable_sql_rewrite: false + distinct: false + replica: false + query_comment: '' + query_tags: { } + exposed_form: + type: basic + options: + submit_button: Apply + reset_button: false + reset_button_label: Reset + exposed_sorts_label: 'Sort by' + expose_sort_order: true + sort_asc_label: Asc + sort_desc_label: Desc + pager: + type: mini + options: + items_per_page: 10 + offset: 0 + id: 0 + total_pages: null + expose: + items_per_page: false + items_per_page_label: 'Items per page' + items_per_page_options: '5, 10, 25, 50' + items_per_page_options_all: false + items_per_page_options_all_label: '- All -' + offset: false + offset_label: Offset + tags: + previous: ‹‹ + next: ›› + style: + type: default + options: + grouping: { } + row_class: '' + default_row_class: true + uses_fields: false + row: + type: fields + options: + inline: { } + separator: '' + hide_empty: false + default_field_elements: true + fields: + fid: + id: fid + table: file_managed + field: fid + relationship: user_to_file + group_type: group + admin_label: '' + label: '' + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: false + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + click_sort_column: value + type: number_integer + settings: + thousand_separator: '' + prefix_suffix: true + group_column: value + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false + entity_type: file + entity_field: fid + plugin_id: field + uuid: + id: uuid + table: users + field: uuid + relationship: none + group_type: group + admin_label: '' + label: '' + exclude: false + alter: + alter_text: false + text: '' + make_link: false + path: '' + absolute: false + external: false + replace_spaces: false + path_case: none + trim_whitespace: false + alt: '' + rel: '' + link_class: '' + prefix: '' + suffix: '' + target: '' + nl2br: false + max_length: 0 + word_boundary: true + ellipsis: true + more_link: false + more_link_text: '' + more_link_path: '' + strip_tags: false + trim: false + preserve_tags: '' + html: false + element_type: '' + element_class: '' + element_label_type: '' + element_label_class: '' + element_label_colon: false + element_wrapper_type: '' + element_wrapper_class: '' + element_default_classes: true + empty: '' + hide_empty: false + empty_zero: false + hide_alter_empty: true + click_sort_column: value + type: string + settings: + link_to_entity: false + group_column: value + group_columns: { } + group_rows: true + delta_limit: 0 + delta_offset: 0 + delta_reversed: false + delta_first_last: false + multi_type: separator + separator: ', ' + field_api_classes: false + entity_type: user + entity_field: uuid + plugin_id: field + filters: { } + sorts: { } + header: { } + footer: { } + empty: { } + relationships: + user_to_file: + id: user_to_file + table: file_usage + field: user_to_file + relationship: none + group_type: group + admin_label: File + required: true + plugin_id: standard + arguments: { } + display_extenders: { } + cache_metadata: + max-age: -1 + contexts: + - url.query_args + - user.permissions + tags: { } diff --git a/core/modules/file/tests/src/Kernel/Views/RelationshipNodeFileDataTest.php b/core/modules/file/tests/src/Kernel/Views/RelationshipNodeFileDataTest.php new file mode 100644 index 00000000000..0fbd0fefcf0 --- /dev/null +++ b/core/modules/file/tests/src/Kernel/Views/RelationshipNodeFileDataTest.php @@ -0,0 +1,196 @@ +installSchema('file', 'file_usage'); + $this->installEntitySchema('node'); + $this->installEntitySchema('file'); + $this->installEntitySchema('user'); + $this->installConfig(['node', 'field', 'filter', 'file_test_views']); + + // Create the node file field and instance. + FieldStorageConfig::create([ + 'entity_type' => 'node', + 'field_name' => 'node_file', + 'type' => 'file', + 'translatable' => '0', + ])->save(); + FieldConfig::create([ + 'label' => 'Node File', + 'description' => '', + 'field_name' => 'node_file', + 'entity_type' => 'node', + 'bundle' => 'file_test', + 'required' => 0, + ])->save(); + } + + /** + * Tests using the views file_to_node relationship. + */ + public function testViewsHandlerRelationshipFileToNode() { + $file1 = File::create([ + 'filename' => 'image-test.jpg', + 'uri' => "public://image-test.jpg", + 'filemime' => 'image/jpeg', + 'created' => 1, + 'changed' => 1, + 'status' => FILE_STATUS_PERMANENT, + ]); + $file1->enforceIsNew(); + file_put_contents($file1->getFileUri(), file_get_contents('core/modules/simpletest/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' => FILE_STATUS_PERMANENT, + ]); + $file2->enforceIsNew(); + file_put_contents($file2->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png')); + $file2->save(); + + $node1 = Node::create([ + 'type' => 'file_test', + 'title' => $this->randomMachineName(8), + 'created' => 1, + 'changed' => 1, + 'status' => NODE_PUBLISHED, + ]); + $node1->save(); + + $node2 = Node::create([ + 'type' => 'file_test', + 'title' => $this->randomMachineName(8), + 'created' => 1, + 'changed' => 1, + 'status' => NODE_PUBLISHED, + 'node_file' => ['target_id' => $file2->id()] + ]); + $node2->save(); + + $view = Views::getView('test_file_to_node'); + $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(), + 'nid' => $node2->id(), + ], + ]; + $column_map = ['fid' => 'fid', 'nid' => 'nid']; + $this->assertIdenticalResultset($view, $expected_result, $column_map); + } + + /** + * Tests using the views node_to_file relationship. + */ + public function testViewsHandlerRelationshipNodeToFile() { + $file1 = File::create([ + 'filename' => 'image-test.jpg', + 'uri' => "public://image-test.jpg", + 'filemime' => 'image/jpeg', + 'created' => 1, + 'changed' => 1, + 'status' => FILE_STATUS_PERMANENT, + ]); + $file1->enforceIsNew(); + file_put_contents($file1->getFileUri(), file_get_contents('core/modules/simpletest/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' => FILE_STATUS_PERMANENT, + ]); + $file2->enforceIsNew(); + file_put_contents($file2->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png')); + $file2->save(); + + $node1 = Node::create([ + 'type' => 'file_test', + 'title' => $this->randomMachineName(8), + 'created' => 1, + 'changed' => 1, + 'status' => NODE_PUBLISHED, + ]); + $node1->save(); + + $node2 = Node::create([ + 'type' => 'file_test', + 'title' => $this->randomMachineName(8), + 'created' => 1, + 'changed' => 1, + 'status' => NODE_PUBLISHED, + 'node_file' => ['target_id' => $file2->id()] + ]); + $node2->save(); + + $view = Views::getView('test_node_to_file'); + $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(), + 'nid' => $node2->id(), + ], + ]; + $column_map = ['fid' => 'fid', 'nid' => 'nid']; + $this->assertIdenticalResultset($view, $expected_result, $column_map); + } + +} diff --git a/core/modules/file/tests/src/Kernel/Views/RelationshipUserFileDataTest.php b/core/modules/file/tests/src/Kernel/Views/RelationshipUserFileDataTest.php new file mode 100644 index 00000000000..5d181fff02a --- /dev/null +++ b/core/modules/file/tests/src/Kernel/Views/RelationshipUserFileDataTest.php @@ -0,0 +1,225 @@ +installSchema('file', 'file_usage'); + $this->installEntitySchema('file'); + $this->installEntitySchema('user'); + $this->installConfig(['field', 'file_test_views']); + + // Create the user profile field and instance. + FieldStorageConfig::create([ + 'entity_type' => 'user', + 'field_name' => 'user_file', + 'type' => 'file', + 'translatable' => '0', + ])->save(); + FieldConfig::create([ + 'label' => 'User File', + 'description' => '', + 'field_name' => 'user_file', + 'entity_type' => 'user', + 'bundle' => 'user', + 'required' => 0, + ])->save(); + + ViewTestData::createTestViews(get_class($this), ['file_test_views']); + } + + /** + * Tests using the views user_to_file relationship. + */ + public function testViewsHandlerRelationshipUserToFile() { + $file1 = File::create([ + 'filename' => 'image-test.jpg', + 'uri' => "public://image-test.jpg", + 'filemime' => 'image/jpeg', + 'created' => 1, + 'changed' => 1, + 'status' => FILE_STATUS_PERMANENT, + ]); + $file1->enforceIsNew(); + file_put_contents($file1->getFileUri(), file_get_contents('core/modules/simpletest/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' => FILE_STATUS_PERMANENT, + ]); + $file2->enforceIsNew(); + file_put_contents($file2->getFileUri(), file_get_contents('core/modules/simpletest/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_user_to_file'); + $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); + } + + /** + * Tests using the views file_to_user relationship. + */ + public function testViewsHandlerRelationshipFileToUser() { + $file1 = File::create([ + 'filename' => 'image-test.jpg', + 'uri' => "public://image-test.jpg", + 'filemime' => 'image/jpeg', + 'created' => 1, + 'changed' => 1, + 'status' => FILE_STATUS_PERMANENT, + ]); + $file1->enforceIsNew(); + file_put_contents($file1->getFileUri(), file_get_contents('core/modules/simpletest/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' => FILE_STATUS_PERMANENT, + ]); + $file2->enforceIsNew(); + file_put_contents($file2->getFileUri(), file_get_contents('core/modules/simpletest/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); + } + + /** + * Tests using the views file relaationship. + */ + public function testViewsHandlerRelationshipUserFileData() { + $file = File::create([ + 'fid' => 2, + 'uid' => 2, + 'filename' => 'image-test.jpg', + 'uri' => "public://image-test.jpg", + 'filemime' => 'image/jpeg', + 'created' => 1, + 'changed' => 1, + 'status' => FILE_STATUS_PERMANENT, + ]); + $file->enforceIsNew(); + file_put_contents($file->getFileUri(), file_get_contents('core/modules/simpletest/files/image-1.png')); + $file->save(); + + $account = User::create([ + 'name' => $this->randomMachineName(8), + 'mail' => $this->randomMachineName(4) . '@' . $this->randomMachineName(4) . '.com', + 'user_file' => ['target_id' => $file->id()], + ]); + $account->save(); + + $view = Views::getView('test_file_user_file_data'); + // Tests \Drupal\taxonomy\Plugin\views\relationship\NodeTermData::calculateDependencies(). + $expected = [ + 'module' => [ + 'file', + 'user', + ], + ]; + $this->assertIdentical($expected, $view->getDependencies()); + $this->executeView($view); + $expected_result = [ + [ + 'file_managed_user__user_file_fid' => '2', + ], + ]; + $column_map = ['file_managed_user__user_file_fid' => 'file_managed_user__user_file_fid']; + $this->assertIdenticalResultset($view, $expected_result, $column_map); + } + +}