Issue #2628230 by Lendude, mohit_aghera, wadmiraal, usrsbn, cilefen, dawehner, nubeli: Adding File Usage "File" relationship results in broken/missing handler

merge-requests/4106/head
Lauri Eskola 2023-06-02 11:11:22 +03:00
parent 3da8c30968
commit b8e7644636
No known key found for this signature in database
GPG Key ID: 382FC0F5B0DF53F8
9 changed files with 1212 additions and 7 deletions

View File

@ -121,6 +121,7 @@ class FileViewsData extends EntityViewsData {
'skip base' => ['node_field_data', 'node_field_revision', 'users_field_data', 'comment_field_data', 'taxonomy_term_field_data'],
'real field' => 'id',
'relationship' => [
'id' => 'standard',
'title' => $this->t('Content'),
'label' => $this->t('Content'),
'base' => 'node_field_data',
@ -137,6 +138,7 @@ class FileViewsData extends EntityViewsData {
'skip base' => ['file_managed', 'users_field_data', 'comment_field_data', 'taxonomy_term_field_data'],
'real field' => 'fid',
'relationship' => [
'id' => 'standard',
'title' => $this->t('File'),
'label' => $this->t('File'),
'base' => 'file_managed',
@ -154,6 +156,7 @@ class FileViewsData extends EntityViewsData {
'skip base' => ['node_field_data', 'node_field_revision', 'users_field_data', 'comment_field_data', 'taxonomy_term_field_data'],
'real field' => 'id',
'relationship' => [
'id' => 'standard',
'title' => $this->t('User'),
'label' => $this->t('User'),
'base' => 'users',
@ -170,6 +173,7 @@ class FileViewsData extends EntityViewsData {
'skip base' => ['file_managed', 'node_field_data', 'node_field_revision', 'comment_field_data', 'taxonomy_term_field_data'],
'real field' => 'fid',
'relationship' => [
'id' => 'standard',
'title' => $this->t('File'),
'label' => $this->t('File'),
'base' => 'file_managed',
@ -187,6 +191,7 @@ class FileViewsData extends EntityViewsData {
'skip base' => ['node_field_data', 'node_field_revision', 'users_field_data', 'comment_field_data', 'taxonomy_term_field_data'],
'real field' => 'id',
'relationship' => [
'id' => 'standard',
'title' => $this->t('Comment'),
'label' => $this->t('Comment'),
'base' => 'comment_field_data',
@ -203,6 +208,7 @@ class FileViewsData extends EntityViewsData {
'skip base' => ['file_managed', 'node_field_data', 'node_field_revision', 'users_field_data', 'taxonomy_term_field_data'],
'real field' => 'fid',
'relationship' => [
'id' => 'standard',
'title' => $this->t('File'),
'label' => $this->t('File'),
'base' => 'file_managed',
@ -220,6 +226,7 @@ class FileViewsData extends EntityViewsData {
'skip base' => ['node_field_data', 'node_field_revision', 'users_field_data', 'comment_field_data', 'taxonomy_term_field_data'],
'real field' => 'id',
'relationship' => [
'id' => 'standard',
'title' => $this->t('Taxonomy Term'),
'label' => $this->t('Taxonomy Term'),
'base' => 'taxonomy_term_data',
@ -236,6 +243,7 @@ class FileViewsData extends EntityViewsData {
'skip base' => ['file_managed', 'node_field_data', 'node_field_revision', 'users_field_data', 'comment_field_data'],
'real field' => 'fid',
'relationship' => [
'id' => 'standard',
'title' => $this->t('File'),
'label' => $this->t('File'),
'base' => 'file_managed',

View File

@ -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

View File

@ -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: { }

View File

@ -0,0 +1,214 @@
langcode: en
status: true
dependencies:
module:
- file
- user
id: test_file_to_user
label: test_file_to_user
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
uuid:
id: uuid
table: users
field: uuid
relationship: file_to_user
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:
file_to_user:
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:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- user.permissions
tags: { }

View File

@ -0,0 +1,219 @@
langcode: en
status: true
dependencies:
module:
- file
- node
- user
id: test_node_to_file
label: test_node_to_file
module: views
description: ''
tag: ''
base_table: node_field_data
base_field: nid
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:
title:
id: title
table: node_field_data
field: title
entity_type: node
entity_field: title
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
settings:
link_to_entity: true
plugin_id: field
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
type: string
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
fid:
id: fid
table: file_managed
field: fid
relationship: node_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
filters: { }
sorts: { }
header: { }
footer: { }
empty: { }
relationships:
node_to_file:
id: node_to_file
table: file_usage
field: node_to_file
relationship: none
group_type: group
admin_label: File
required: true
plugin_id: standard
arguments: { }
display_extenders: { }
cache_metadata:
max-age: -1
contexts:
- 'languages:language_content'
- 'languages:language_interface'
- url.query_args
- 'user.node_grants:view'
- user.permissions
tags: { }

View File

@ -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: { }

View File

@ -0,0 +1,191 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\file\Kernel\Views;
use Drupal\field\Entity\FieldConfig;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\views\Tests\ViewTestData;
use Drupal\views\Views;
use Drupal\field\Entity\FieldStorageConfig;
/**
* Tests file on node relationship handler.
*
* @group file
*/
class RelationshipNodeFileDataTest extends ViewsKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = ['field', 'file', 'file_test_views', 'node', 'text'];
/**
* Views used by this test.
*
* @var array
*/
public static $testViews = ['test_file_to_node', 'test_node_to_file'];
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE): void {
parent::setUp($import_test_views);
$this->installSchema('file', 'file_usage');
$this->installEntitySchema('node');
$this->installEntitySchema('file');
$this->installEntitySchema('user');
$this->installConfig(['node', 'field', '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();
ViewTestData::createTestViews(static::class, ['file_test_views']);
}
/**
* Tests using the views file_to_node relationship.
*/
public function testViewsHandlerRelationshipFileToNode(): 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();
$node1 = Node::create([
'type' => 'file_test',
'title' => $this->randomMachineName(8),
'created' => 1,
'changed' => 1,
'status' => NodeInterface::PUBLISHED,
]);
$node1->save();
$node2 = Node::create([
'type' => 'file_test',
'title' => $this->randomMachineName(8),
'created' => 1,
'changed' => 1,
'status' => NodeInterface::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(): 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();
$node1 = Node::create([
'type' => 'file_test',
'title' => $this->randomMachineName(8),
'created' => 1,
'changed' => 1,
'status' => NodeInterface::PUBLISHED,
]);
$node1->save();
$node2 = Node::create([
'type' => 'file_test',
'title' => $this->randomMachineName(8),
'created' => 1,
'changed' => 1,
'status' => NodeInterface::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);
}
}

View File

@ -1,12 +1,16 @@
<?php
declare(strict_types=1);
namespace Drupal\Tests\file\Kernel\Views;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\Entity\File;
use Drupal\KernelTests\KernelTestBase;
use Drupal\file\FileInterface;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\Tests\views\Kernel\ViewsKernelTestBase;
use Drupal\user\Entity\User;
use Drupal\views\Tests\ViewResultAssertionTrait;
use Drupal\views\Views;
use Drupal\views\Tests\ViewTestData;
@ -16,7 +20,7 @@ use Drupal\views\Tests\ViewTestData;
*
* @group file
*/
class RelationshipUserFileDataTest extends KernelTestBase {
class RelationshipUserFileDataTest extends ViewsKernelTestBase {
use UserCreationTrait;
use ViewResultAssertionTrait;
@ -38,15 +42,14 @@ class RelationshipUserFileDataTest extends KernelTestBase {
*
* @var array
*/
public static $testViews = ['test_file_user_file_data'];
public static $testViews = ['test_file_user_file_data', 'test_user_to_file', 'test_file_to_user'];
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
protected function setUp($import_test_views = TRUE): void {
parent::setUp($import_test_views);
$this->installSchema('system', ['sequences']);
$this->installSchema('file', ['file_usage']);
$this->installEntitySchema('user');
$this->installEntitySchema('file');
@ -68,12 +71,13 @@ class RelationshipUserFileDataTest extends KernelTestBase {
])->save();
ViewTestData::createTestViews(static::class, ['file_test_views']);
$this->installConfig(['field', 'file_test_views']);
}
/**
* Tests using the views file relationship.
*/
public function testViewsHandlerRelationshipUserFileData() {
public function testViewsHandlerRelationshipUserFileData(): void {
$file = File::create([
'fid' => 2,
'uid' => 2,
@ -110,4 +114,114 @@ class RelationshipUserFileDataTest extends KernelTestBase {
$this->assertIdenticalResultset($view, $expected_result, $column_map);
}
/**
* Tests using the views user_to_file relationship.
*/
public function testViewsHandlerRelationshipUserToFile(): 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_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(): 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);
}
}