Skip to content

Commit

Permalink
Explicitly acknowledge the media thumbnail file.
Browse files Browse the repository at this point in the history
Better than being left thinking there's a bug where we have
undocumentedly expected there to be a file to exist that we didn't
explicitly create in our `::setUp()`.
  • Loading branch information
adam-vessey committed Mar 7, 2024
1 parent 5362f87 commit 7f4e382
Showing 1 changed file with 73 additions and 12 deletions.
85 changes: 73 additions & 12 deletions tests/src/Kernel/EmbargoAccessQueryTaggingAlterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Drupal\embargo\EmbargoInterface;
use Drupal\file\FileInterface;
use Drupal\media\Entity\Media;
use Drupal\media\MediaInterface;
use Drupal\node\NodeInterface;
use Drupal\Tests\islandora_test_support\Traits\DatabaseQueryTestTraits;
Expand Down Expand Up @@ -65,6 +66,35 @@ class EmbargoAccessQueryTaggingAlterTest extends EmbargoKernelTestBase {
*/
protected FileInterface $unembargoedFile;

/**
* Unassociated node from ::setUp().
*
* @var \Drupal\node\NodeInterface
*/
protected NodeInterface $unassociatedNode;

/**
* Unassociated media from ::setUp().
*
* @var \Drupal\media\MediaInterface
*/
protected MediaInterface $unassociatedMedia;

/**
* Unassociated file from ::setUp().
*
* @var \Drupal\file\FileInterface
*/
protected FileInterface $unassociatedFile;

/**
* Lazily created "default thumbnail" image file for (file) media.
*
* @var \Drupal\file\FileInterface
* @see https://git.drupalcode.org/project/drupal/-/blob/cd2c8e49c861a70b0f39b17c01051b16fd6a2662/core/modules/media/src/Entity/Media.php#L203-208
*/
protected FileInterface $mediaTypeDefaultFile;

/**
* {@inheritdoc}
*/
Expand All @@ -78,6 +108,19 @@ public function setUp(): void {

$this->unembargoedNode = $this->createNode();
$this->unembargoedMedia = $this->createMedia($this->unembargoedFile = $this->createFile(), $this->unembargoedNode);

$this->unassociatedNode = $this->createNode();
$this->unassociatedMedia = Media::create([
'bundle' => $this->createMediaType('file', ['id' => 'file_two'])->id(),
])->setPublished();
$this->unassociatedMedia->save();
$this->unassociatedFile = $this->createFile();

// XXX: Media lazily creates a "default thumbnail" image file by default.
// @see https://git.drupalcode.org/project/drupal/-/blob/cd2c8e49c861a70b0f39b17c01051b16fd6a2662/core/modules/media/src/Entity/Media.php#L203-208
$files = $this->storage('file')->loadByProperties(['filename' => 'generic.png']);
$this->assertCount(1, $files, 'only the one generic file.');
$this->mediaTypeDefaultFile = reset($files);
}

/**
Expand All @@ -88,8 +131,11 @@ public function setUp(): void {
public function testEmbargoNodeQueryAlterAccess() {
$query = $this->generateNodeSelectAccessQuery($this->user);
$result = $query->execute()->fetchAll();
$this->assertCount(1, $result, 'User can only view non-embargoed node.');
$this->assertEquals([$this->unembargoedNode->id()], array_column($result, 'nid'));
$this->assertCount(2, $result, 'User can only view non-embargoed nodes.');
$this->assertEqualsCanonicalizing([
$this->unembargoedNode->id(),
$this->unassociatedNode->id(),
], array_column($result, 'nid'));
}

/**
Expand All @@ -100,8 +146,11 @@ public function testEmbargoNodeQueryAlterAccess() {
public function testNodeEmbargoReferencedMediaAccessQueryAlterAccessDenied() {
$query = $this->generateMediaSelectAccessQuery($this->user);
$result = $query->execute()->fetchAll();
$this->assertCount(1, $result, 'Media of embargoed nodes cannot be viewed');
$this->assertEquals([$this->unembargoedMedia->id()], array_column($result, 'mid'));
$this->assertCount(2, $result, 'Media of embargoed nodes cannot be viewed');
$this->assertEqualsCanonicalizing([
$this->unembargoedMedia->id(),
$this->unassociatedMedia->id(),
], array_column($result, 'mid'));
}

/**
Expand All @@ -112,8 +161,12 @@ public function testNodeEmbargoReferencedMediaAccessQueryAlterAccessDenied() {
public function testNodeEmbargoReferencedFileAccessQueryAlterAccessDenied() {
$query = $this->generateFileSelectAccessQuery($this->user);
$result = $query->execute()->fetchAll();
$this->assertCount(1, $result, 'File of embargoed nodes cannot be viewed');
$this->assertEquals([$this->unembargoedFile->id()], array_column($result, 'fid'));
$this->assertCount(3, $result, 'File of embargoed nodes cannot be viewed');
$this->assertEqualsCanonicalizing([
$this->unembargoedFile->id(),
$this->unassociatedFile->id(),
$this->mediaTypeDefaultFile->id(),
], array_column($result, 'fid'));
}

/**
Expand All @@ -128,10 +181,11 @@ public function testDeletedNodeEmbargoNodeAccessQueryAlterAccessAllowed() {
$query = $this->generateNodeSelectAccessQuery($this->user);

$result = $query->execute()->fetchAll();
$this->assertCount(2, $result, 'Non embargoed nodes can be viewed');
$this->assertCount(3, $result, 'Non embargoed nodes can be viewed');
$this->assertEqualsCanonicalizing([
$this->embargoedNode->id(),
$this->unembargoedNode->id(),
$this->unassociatedNode->id(),
], array_column($result, 'nid'));
}

Expand All @@ -146,11 +200,12 @@ public function testDeletedNodeEmbargoMediaAccessQueryAlterAccessAllowed() {
$this->embargo->delete();
$query = $this->generateMediaSelectAccessQuery($this->user);
$result = $query->execute()->fetchAll();
$this->assertCount(2, $result,
$this->assertCount(3, $result,
'Media of non embargoed nodes can be viewed');
$this->assertEqualsCanonicalizing([
$this->embargoedMedia->id(),
$this->unembargoedMedia->id(),
$this->unassociatedMedia->id(),
], array_column($result, 'mid'));
}

Expand All @@ -166,11 +221,13 @@ public function testDeletedNodeEmbargoFileAccessQueryAlterAccessAllowed() {
$query = $this->generateFileSelectAccessQuery($this->user);

$result = $query->execute()->fetchAll();
$this->assertCount(2, $result,
$this->assertCount(4, $result,
'Files of non embargoed nodes can be viewed');
$this->assertEqualsCanonicalizing([
$this->embargoedFile->id(),
$this->unembargoedFile->id(),
$this->unassociatedFile->id(),
$this->mediaTypeDefaultFile->id(),
], array_column($result, 'fid'));
}

Expand All @@ -184,9 +241,12 @@ public function testPublishScheduledEmbargoAccess() {
$this->setEmbargoFutureUnpublishDate($this->embargo);

$result = $this->generateNodeSelectAccessQuery($this->user)->execute()->fetchAll();
$this->assertCount(1, $result,
$this->assertCount(2, $result,
'Node is still embargoed.');
$this->assertEqualsCanonicalizing([$this->unembargoedNode->id()], array_column($result, 'nid'));
$this->assertEqualsCanonicalizing([
$this->unembargoedNode->id(),
$this->unassociatedNode->id(),
], array_column($result, 'nid'));
}

/**
Expand All @@ -200,11 +260,12 @@ public function testUnpublishScheduledEmbargoAccess() {
$this->setEmbargoPastUnpublishDate($this->embargo);

$result = $this->generateNodeSelectAccessQuery($this->user)->execute()->fetchAll();
$this->assertCount(2, $result,
$this->assertCount(3, $result,
'Embargo has been unpublished.');
$this->assertEqualsCanonicalizing([
$this->embargoedNode->id(),
$this->unembargoedNode->id(),
$this->unassociatedNode->id(),
], array_column($result, 'nid'));
}

Expand Down

0 comments on commit 7f4e382

Please sign in to comment.