Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(github): Test in PHP development mode #1362

Merged
merged 4 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ jobs:
php-version: ${{ matrix.php-versions }}
# Disable Xdebug for better performance.
coverage: none
ini-file: development
extensions: ${{ env.extensions }}

- name: Get composer cache directory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,50 @@ class ImageDerivativeTest extends GraphQLTestBase {
*/
protected static $modules = ['image', 'file'];

/**
* The file system URI under test.
*
* @var string
*/
protected $fileUri;

/**
* The file entity mock.
*
* @var \Drupal\file\FileInterface
*/
protected $file;

/**
* The image style for testing.
*
* @var \Drupal\image\Entity\ImageStyle
*/
protected $style;

/**
* A file entity mock that returns FALSE on access checking.
*
* @var \Drupal\file\FileInterface
*/
protected $fileNotAccessible;

/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();

$this->file_uri = 'public://test.jpg';
$this->fileUri = 'public://test.jpg';

$this->file = $this->getMockBuilder(FileInterface::class)
->disableOriginalConstructor()
->getMock();

$this->file->method('getFileUri')->willReturn($this->file_uri);
$this->file->method('getFileUri')->willReturn($this->fileUri);
$this->file->method('access')->willReturn((new AccessResultAllowed())->addCacheTags(['test_tag']));
$this->file->width = 600;
$this->file->height = 400;
@$this->file->width = 600;
@$this->file->height = 400;

$this->style = ImageStyle::create(['name' => 'test_style']);
$effect = [
Expand All @@ -49,11 +77,11 @@ public function setUp(): void {
$this->style->addImageEffect($effect);
$this->style->save();

$this->file_not_accessible = $this->getMockBuilder(FileInterface::class)
$this->fileNotAccessible = $this->getMockBuilder(FileInterface::class)
->disableOriginalConstructor()
->getMock();

$this->file_not_accessible->method('access')->willReturn((new AccessResultForbidden())->addCacheTags(['test_tag_forbidden']));
$this->fileNotAccessible->method('access')->willReturn((new AccessResultForbidden())->addCacheTags(['test_tag_forbidden']));
}

/**
Expand All @@ -69,7 +97,7 @@ public function testImageDerivative(): void {

$this->assertEquals(
[
'url' => $this->style->buildUrl($this->file_uri),
'url' => $this->style->buildUrl($this->fileUri),
'width' => 300,
'height' => 200,
],
Expand All @@ -83,7 +111,7 @@ public function testImageDerivative(): void {
// Test that we don't get the derivative if we don't have access to the
// original file, but we still get the access result cache tags.
$result = $this->executeDataProducer('image_derivative', [
'entity' => $this->file_not_accessible,
'entity' => $this->fileNotAccessible,
'style' => 'test_style',
]);

Expand Down
28 changes: 24 additions & 4 deletions tests/src/Kernel/DataProducer/Entity/Fields/Image/ImageUrlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,32 @@
*/
class ImageUrlTest extends GraphQLTestBase {

/**
* The file entity mock.
*
* @var \Drupal\file\FileInterface
*/
protected $file;

/**
* A file entity mock that returns FALSE on access checking.
*
* @var \Drupal\file\FileInterface
*/
protected $fileNotAccessible;

/**
* The generated file URI.
*
* @var string
*/
protected $fileUri;

/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();
$this->dataProducerManager = $this->container->get('plugin.manager.graphql.data_producer');

$this->fileUri = \Drupal::service('file_url_generator')->generateAbsoluteString('public://test.jpg');

Expand All @@ -30,11 +50,11 @@ public function setUp(): void {
$this->file->method('getFileUri')->willReturn($this->fileUri);
$this->file->method('access')->willReturn((new AccessResultAllowed())->addCacheTags(['test_tag']));

$this->file_not_accessible = $this->getMockBuilder(FileInterface::class)
$this->fileNotAccessible = $this->getMockBuilder(FileInterface::class)
->disableOriginalConstructor()
->getMock();

$this->file_not_accessible->method('access')->willReturn((new AccessResultForbidden())->addCacheTags(['test_tag_forbidden']));
$this->fileNotAccessible->method('access')->willReturn((new AccessResultForbidden())->addCacheTags(['test_tag_forbidden']));
}

/**
Expand All @@ -53,7 +73,7 @@ public function testImageUrl(): void {
// Test that we do not get a file we don't have access to, but the cache
// tags are still added.
$result = $this->executeDataProducer('image_url', [
'entity' => $this->file_not_accessible,
'entity' => $this->fileNotAccessible,
]);

$this->assertNull($result);
Expand Down
14 changes: 0 additions & 14 deletions tests/src/Kernel/DataProducer/EntityMultipleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\UserInterface;
use Drupal\node\Entity\NodeType;
use Drupal\node\Entity\Node;

Expand Down Expand Up @@ -42,18 +40,6 @@ class EntityMultipleTest extends GraphQLTestBase {
public function setUp(): void {
parent::setUp();

$this->entity = $this->getMockBuilder(NodeInterface::class)
->disableOriginalConstructor()
->getMock();

$this->entity_interface = $this->getMockBuilder(EntityInterface::class)
->disableOriginalConstructor()
->getMock();

$this->user = $this->getMockBuilder(UserInterface::class)
->disableOriginalConstructor()
->getMock();

$content_type = NodeType::create([
'type' => 'lorem',
'name' => 'ipsum',
Expand Down
41 changes: 20 additions & 21 deletions tests/src/Kernel/DataProducer/EntityReferenceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,8 @@
use Drupal\Tests\field\Traits\EntityReferenceTestTrait;
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\node\Entity\NodeType;
use Drupal\node\Entity\Node;
use Drupal\user\UserInterface;

/**
* Tests the entity_reference data producers.
Expand All @@ -19,24 +16,26 @@
class EntityReferenceTest extends GraphQLTestBase {
use EntityReferenceTestTrait;

/**
* Test node that will be referenced.
*
* @var \Drupal\node\Entity\Node
*/
protected $referencedNode;

/**
* Test node.
*
* @var \Drupal\node\Entity\Node
*/
protected $node;

/**
* {@inheritdoc}
*/
public function setUp(): void {
parent::setUp();

$this->entity = $this->getMockBuilder(NodeInterface::class)
->disableOriginalConstructor()
->getMock();

$this->entity_interface = $this->getMockBuilder(EntityInterface::class)
->disableOriginalConstructor()
->getMock();

$this->user = $this->getMockBuilder(UserInterface::class)
->disableOriginalConstructor()
->getMock();

$content_type1 = NodeType::create([
'type' => 'test1',
'name' => 'ipsum1',
Expand All @@ -51,19 +50,19 @@ public function setUp(): void {

$this->createEntityReferenceField('node', 'test1', 'field_test1_to_test2', 'test1 lable', 'node', 'default', [], FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);

$this->referenced_node = Node::create([
$this->referencedNode = Node::create([
'title' => 'Dolor2',
'type' => 'test2',
]);
$this->referenced_node->save();
$this->referenced_node
$this->referencedNode->save();
$this->referencedNode
->addTranslation('fr', ['title' => 'Dolor2 French'])
->save();

$this->node = Node::create([
'title' => 'Dolor',
'type' => 'test1',
'field_test1_to_test2' => $this->referenced_node->id(),
'field_test1_to_test2' => $this->referencedNode->id(),
]);
$this->node->save();
}
Expand All @@ -78,7 +77,7 @@ public function testResolveEntityReference(): void {
'access' => TRUE,
'access_operation' => 'view',
]);
$this->assertEquals($this->referenced_node->id(), reset($result)->id());
$this->assertEquals($this->referencedNode->id(), reset($result)->id());
$this->assertEquals('Dolor2', reset($result)->label());

$result = $this->executeDataProducer('entity_reference', [
Expand All @@ -88,7 +87,7 @@ public function testResolveEntityReference(): void {
'access_operation' => 'view',
'language' => 'fr',
]);
$this->assertEquals($this->referenced_node->id(), reset($result)->id());
$this->assertEquals($this->referencedNode->id(), reset($result)->id());
$this->assertEquals('Dolor2 French', reset($result)->label());
}

Expand Down
53 changes: 44 additions & 9 deletions tests/src/Kernel/DataProducer/EntityTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,41 @@ class EntityTest extends GraphQLTestBase {
*/
protected $node;

/**
* Mocked test entity.
*
* @var \Drupal\node\NodeInterface|\PHPUnit\Framework\MockObject\MockObject
*/
protected $entity;

/**
* Mocked test entity interface.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entityInterface;

/**
* Mocked test user.
*
* @var \Drupal\user\UserInterface
*/
protected $user;

/**
* Translated test entity.
*
* @var \Drupal\node\NodeInterface
*/
protected $translationFr;

/**
* Translated test entity.
*
* @var \Drupal\node\NodeInterface
*/
protected $translationDe;

/**
* {@inheritdoc}
*/
Expand All @@ -34,7 +69,7 @@ public function setUp(): void {
->disableOriginalConstructor()
->getMock();

$this->entity_interface = $this->getMockBuilder(EntityInterface::class)
$this->entityInterface = $this->getMockBuilder(EntityInterface::class)
->disableOriginalConstructor()
->getMock();

Expand Down Expand Up @@ -64,11 +99,11 @@ public function setUp(): void {
]);
$this->node->save();

$this->translation_fr = $this->node->addTranslation('fr', ['title' => 'sit amet fr']);
$this->translation_fr->save();
$this->translationFr = $this->node->addTranslation('fr', ['title' => 'sit amet fr']);
$this->translationFr->save();

$this->translation_de = $this->node->addTranslation('de', ['title' => 'sit amet de']);
$this->translation_de->save();
$this->translationDe = $this->node->addTranslation('de', ['title' => 'sit amet de']);
$this->translationDe->save();

\Drupal::service('content_translation.manager')->setEnabled('node', 'lorem', TRUE);
}
Expand Down Expand Up @@ -103,7 +138,7 @@ public function testResolveChanged(): void {

$this->assertNull($this->executeDataProducer('entity_changed', [
'format' => 'Y-m-d',
'entity' => $this->entity_interface,
'entity' => $this->entityInterface,
]));
}

Expand All @@ -122,7 +157,7 @@ public function testResolveCreated(): void {

$this->assertNull($this->executeDataProducer('entity_created', [
'format' => 'Y-m-d',
'entity' => $this->entity_interface,
'entity' => $this->entityInterface,
]));
}

Expand Down Expand Up @@ -199,7 +234,7 @@ public function testResolveOwner(): void {
]));

$this->assertNull($this->executeDataProducer('entity_owner', [
'entity' => $this->entity_interface,
'entity' => $this->entityInterface,
]));
}

Expand Down Expand Up @@ -229,7 +264,7 @@ public function testResolvePublished(): void {
]));

$this->assertNull($this->executeDataProducer('entity_published', [
'entity' => $this->entity_interface,
'entity' => $this->entityInterface,
]));
}

Expand Down
Loading