You are here

AnimatedGifFunctionalTestBase.php in Animated GIF 8

File

tests/src/Functional/AnimatedGifFunctionalTestBase.php
View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\animated_gif\Functional;

use Drupal\Core\File\FileSystemInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\file\FileInterface;
use Drupal\Tests\BrowserTestBase;

/**
 * Base class for Animated GIF functional tests.
 */
abstract class AnimatedGifFunctionalTestBase extends BrowserTestBase {

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'animated_gif',
    'node',
    'file',
    'image',
    'field_ui',
  ];

  /**
   * File system service.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The display repository service.
   *
   * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
   */
  protected $displayRepository;

  /**
   * A user with administration permissions.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $adminUser;

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

    /** @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface $displayRepository */
    $this->displayRepository = $this->container
      ->get('entity_display.repository');
    $this->fileSystem = $this->container
      ->get('file_system');
    $this->entityTypeManager = $this->container
      ->get('entity_type.manager');
    $this->adminUser = $this
      ->drupalCreateUser([
      'access content',
      'access administration pages',
      'administer content types',
      'administer node fields',
      'administer node display',
      'administer nodes',
      'bypass node access',
    ]);
    $this
      ->drupalCreateContentType([
      'type' => 'article',
      'name' => 'Article',
    ]);
  }

  /**
   * Helper method to get the tested file.
   *
   * @param string $fileName
   *   The name of the file.
   * @param string $fileUri
   *   The Uri of the file.
   *
   * @return \Drupal\file\FileInterface
   *   Return a file.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  protected function getTestFile(string $fileName, string $fileUri) : FileInterface {

    // Copy the source file to public directory.
    $source = drupal_get_path('module', 'animated_gif');
    $source .= '/tests/images/' . $fileName;
    $this->fileSystem
      ->copy($source, $fileUri, FileSystemInterface::EXISTS_REPLACE);

    /** @var \Drupal\file\FileInterface $file */
    $file = $this->entityTypeManager
      ->getStorage('file')
      ->create([
      'filename' => $fileName,
      'filemime' => 'image/gif',
      'uri' => $fileUri,
    ]);
    return $file;
  }

  /**
   * Helper method to create the image field.
   *
   * @param string $entityType
   *   The entity type.
   * @param string $bundle
   *   The bundle name.
   * @param string $fieldName
   *   The field name.
   *
   * @throws \Drupal\Core\Entity\EntityStorageException
   */
  protected function createFileField(string $entityType, string $bundle, string $fieldName) : void {
    if (!FieldStorageConfig::loadByName($entityType, $fieldName)) {
      FieldStorageConfig::create([
        'field_name' => $fieldName,
        'entity_type' => $entityType,
        'type' => 'image',
        'settings' => [
          'alt_field_required' => 0,
        ],
        'cardinality' => 1,
      ])
        ->save();
    }
    if (!FieldConfig::loadByName($entityType, $bundle, $fieldName)) {
      FieldConfig::create([
        'field_name' => $fieldName,
        'entity_type' => $entityType,
        'bundle' => $bundle,
        'label' => $fieldName,
      ])
        ->save();
    }
  }

}

Classes

Namesort descending Description
AnimatedGifFunctionalTestBase Base class for Animated GIF functional tests.