ImageDerivativeTest.php in GraphQL 8.4        
                          
                  
                        
  
  
  
  
  
File
  tests/src/Kernel/DataProducer/Entity/Fields/Image/ImageDerivativeTest.php
  
    View source  
  <?php
namespace Drupal\Tests\graphql\Kernel\DataProducer\Entity\Fields\Image;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Access\AccessResultForbidden;
use Drupal\file\FileInterface;
use Drupal\image\Entity\ImageStyle;
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
class ImageDerivativeTest extends GraphQLTestBase {
  
  protected static $modules = [
    'image',
    'file',
  ];
  
  public function setUp() : void {
    parent::setUp();
    $this->file_uri = 'public://test.jpg';
    $this->file = $this
      ->getMockBuilder(FileInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->file
      ->method('getFileUri')
      ->willReturn($this->file_uri);
    $this->file
      ->method('access')
      ->willReturn((new AccessResultAllowed())
      ->addCacheTags([
      'test_tag',
    ]));
    $this->file->width = 600;
    $this->file->height = 400;
    $this->style = ImageStyle::create([
      'name' => 'test_style',
    ]);
    $effect = [
      'id' => 'image_resize',
      'data' => [
        'width' => 300,
        'height' => 200,
      ],
    ];
    $this->style
      ->addImageEffect($effect);
    $this->style
      ->save();
    $this->file_not_accessible = $this
      ->getMockBuilder(FileInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->file_not_accessible
      ->method('access')
      ->willReturn((new AccessResultForbidden())
      ->addCacheTags([
      'test_tag_forbidden',
    ]));
  }
  
  public function testImageDerivative() : void {
    
    $result = $this
      ->executeDataProducer('image_derivative', [
      'entity' => $this->file,
      'style' => 'test_style',
    ]);
    $this
      ->assertEquals([
      'url' => $this->style
        ->buildUrl($this->file_uri),
      'width' => 300,
      'height' => 200,
    ], $result);
    
    $result = $this
      ->executeDataProducer('image_derivative', [
      'entity' => $this->file_not_accessible,
      'style' => 'test_style',
    ]);
    $this
      ->assertNull($result);
    
  }
}