class ToolkitTestBase

Same name and namespace in other branches
  1. 8.9.x core/modules/system/src/Tests/Image/ToolkitTestBase.php \Drupal\system\Tests\Image\ToolkitTestBase
  2. 8.9.x core/tests/Drupal/FunctionalTests/Image/ToolkitTestBase.php \Drupal\FunctionalTests\Image\ToolkitTestBase

Base class for image manipulation testing.

Hierarchy

Expanded class hierarchy of ToolkitTestBase

Deprecated

in drupal:9.1.0 and is removed from drupal:10.0.0. There is no replacement provided as functional test base class because toolkit operations should be tested as kernel tests. ToolkitTestTrait trait has been added to provide a similar functionality for toolkit kernel tests.

See also

https://www.drupal.org/node/3035573

\Drupal\Tests\Traits\Core\Image\ToolkitTestTrait

File

core/tests/Drupal/FunctionalTests/Image/ToolkitTestBase.php, line 22

Namespace

Drupal\FunctionalTests\Image
View source
abstract class ToolkitTestBase extends BrowserTestBase {
  use TestFileCreationTrait {
    getTestFiles as drupalGetTestFiles;
  }
  
  /**
   * Modules to enable.
   *
   * @var array
   */
  protected static $modules = [
    'image_test',
  ];
  
  /**
   * The URI for the file.
   *
   * @var string
   */
  protected $file;
  
  /**
   * The image factory service.
   *
   * @var \Drupal\Core\Image\ImageFactory
   */
  protected $imageFactory;
  
  /**
   * The image object for the test file.
   *
   * @var \Drupal\Core\Image\ImageInterface
   */
  protected $image;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    // Set the image factory service.
    $this->imageFactory = $this->container
      ->get('image.factory');
    // Pick a file for testing.
    $file = current($this->drupalGetTestFiles('image'));
    $this->file = $file->uri;
    // Setup a dummy image to work with.
    $this->image = $this->getImage();
    // Clear out any hook calls.
    $this->imageTestReset();
  }
  
  /**
   * Sets up an image with the custom toolkit.
   *
   * @return \Drupal\Core\Image\ImageInterface
   *   The image object.
   */
  protected function getImage() {
    $image = $this->imageFactory
      ->get($this->file, 'test');
    $this->assertTrue($image->isValid(), 'Image file was parsed.');
    return $image;
  }
  
  /**
   * Tests that only allowed image toolkit operations are called.
   *
   * @param $expected
   *   Array with string containing with the operation name, e.g. 'load',
   *   'save', 'crop', etc.
   */
  public function assertToolkitOperationsCalled(array $expected) {
    // If one of the image operations is expected, apply should be expected as
    // well.
    $operations = [
      'resize',
      'rotate',
      'crop',
      'desaturate',
      'create_new',
      'scale',
      'scale_and_crop',
      'my_operation',
      'convert',
    ];
    if (count(array_intersect($expected, $operations)) > 0 && !in_array('apply', $expected)) {
      $expected[] = 'apply';
    }
    // Determine which operations were called.
    $actual = array_keys(array_filter($this->imageTestGetAllCalls()));
    // Determine if there were any expected that were not called.
    $uncalled = array_diff($expected, $actual);
    if (count($uncalled)) {
      $this->assertTrue(FALSE, new FormattableMarkup('Expected operations %expected to be called but %uncalled was not called.', [
        '%expected' => implode(', ', $expected),
        '%uncalled' => implode(', ', $uncalled),
      ]));
    }
    else {
      $this->assertTrue(TRUE, new FormattableMarkup('All the expected operations were called: %expected', [
        '%expected' => implode(', ', $expected),
      ]));
    }
    // Determine if there were any unexpected calls.
    // If all unexpected calls are operations and apply was expected, we do not
    // count it as an error.
    $unexpected = array_diff($actual, $expected);
    if (count($unexpected) && (!in_array('apply', $expected) || count(array_intersect($unexpected, $operations)) !== count($unexpected))) {
      $this->assertTrue(FALSE, new FormattableMarkup('Unexpected operations were called: %unexpected.', [
        '%unexpected' => implode(', ', $unexpected),
      ]));
    }
    else {
      $this->assertTrue(TRUE, 'No unexpected operations were called.');
    }
  }
  
  /**
   * Resets/initializes the history of calls to the test toolkit functions.
   */
  protected function imageTestReset() {
    // Keep track of calls to these operations
    $results = [
      'parseFile' => [],
      'save' => [],
      'settings' => [],
      'apply' => [],
      'resize' => [],
      'rotate' => [],
      'crop' => [],
      'desaturate' => [],
      'create_new' => [],
      'scale' => [],
      'scale_and_crop' => [],
      'convert' => [],
    ];
    \Drupal::state()->set('image_test.results', $results);
  }
  
  /**
   * Gets an array of calls to the test toolkit.
   *
   * @return array
   *   An array keyed by operation name ('parseFile', 'save', 'settings',
   *   'resize', 'rotate', 'crop', 'desaturate') with values being arrays of
   *   parameters passed to each call.
   */
  protected function imageTestGetAllCalls() {
    return \Drupal::state()->get('image_test.results', []);
  }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary
AssertLegacyTrait::assert Deprecated protected function
AssertLegacyTrait::assertCacheTag Deprecated protected function Asserts whether an expected cache tag was present in the last response.
AssertLegacyTrait::assertElementNotPresent Deprecated protected function Asserts that the element with the given CSS selector is not present.
AssertLegacyTrait::assertElementPresent Deprecated protected function Asserts that the element with the given CSS selector is present.
AssertLegacyTrait::assertEqual Deprecated protected function
AssertLegacyTrait::assertEscaped Deprecated protected function Passes if the raw text IS found escaped on the loaded page, fail otherwise.
AssertLegacyTrait::assertField Deprecated protected function Asserts that a field exists with the given name or ID.
AssertLegacyTrait::assertFieldById Deprecated protected function Asserts that a field exists with the given ID and value.
AssertLegacyTrait::assertFieldByName Deprecated protected function Asserts that a field exists with the given name and value.
AssertLegacyTrait::assertFieldByXPath Deprecated protected function Asserts that a field exists in the current page by the given XPath.
AssertLegacyTrait::assertFieldChecked Deprecated protected function Asserts that a checkbox field in the current page is checked.
AssertLegacyTrait::assertFieldsByValue Deprecated protected function Asserts that a field exists in the current page with a given Xpath result.
AssertLegacyTrait::assertHeader Deprecated protected function Checks that current response header equals value.
AssertLegacyTrait::assertIdentical Deprecated protected function
AssertLegacyTrait::assertIdenticalObject Deprecated protected function
AssertLegacyTrait::assertLink Deprecated protected function Passes if a link with the specified label is found.
AssertLegacyTrait::assertLinkByHref Deprecated protected function Passes if a link containing a given href (part) is found.
AssertLegacyTrait::assertNoCacheTag Deprecated protected function Asserts whether an expected cache tag was absent in the last response.
AssertLegacyTrait::assertNoEscaped Deprecated protected function Passes if the raw text is not found escaped on the loaded page.
AssertLegacyTrait::assertNoField Deprecated protected function Asserts that a field does NOT exist with the given name or ID.
AssertLegacyTrait::assertNoFieldById Deprecated protected function Asserts that a field does not exist with the given ID and value.
AssertLegacyTrait::assertNoFieldByName Deprecated protected function Asserts that a field does not exist with the given name and value.
AssertLegacyTrait::assertNoFieldByXPath Deprecated protected function Asserts that a field does not exist or its value does not match, by XPath.
AssertLegacyTrait::assertNoFieldChecked Deprecated protected function Asserts that a checkbox field in the current page is not checked.
AssertLegacyTrait::assertNoLink Deprecated protected function Passes if a link with the specified label is not found.
AssertLegacyTrait::assertNoLinkByHref Deprecated protected function Passes if a link containing a given href (part) is not found.
AssertLegacyTrait::assertNoOption Deprecated protected function Asserts that a select option does NOT exist in the current page.
AssertLegacyTrait::assertNoPattern Deprecated protected function Triggers a pass if the Perl regex pattern is not found in the raw content.
AssertLegacyTrait::assertNoRaw Deprecated protected function Passes if the raw text IS not found on the loaded page, fail otherwise.
AssertLegacyTrait::assertNotEqual Deprecated protected function
AssertLegacyTrait::assertNoText Deprecated protected function Passes if the page (with HTML stripped) does not contains the text.
AssertLegacyTrait::assertNotIdentical Deprecated protected function
AssertLegacyTrait::assertNoUniqueText Deprecated protected function Passes if the text is found MORE THAN ONCE on the text version of the page.
AssertLegacyTrait::assertOption Deprecated protected function Asserts that a select option in the current page exists.
AssertLegacyTrait::assertOptionByText Deprecated protected function Asserts that a select option with the visible text exists.
AssertLegacyTrait::assertOptionSelected Deprecated protected function Asserts that a select option in the current page is checked.
AssertLegacyTrait::assertPattern Deprecated protected function Triggers a pass if the Perl regex pattern is found in the raw content.
AssertLegacyTrait::assertRaw Deprecated protected function Passes if the raw text IS found on the loaded page, fail otherwise.
AssertLegacyTrait::assertResponse Deprecated protected function Asserts the page responds with the specified response code.
AssertLegacyTrait::assertText Deprecated protected function Passes if the page (with HTML stripped) contains the text.
AssertLegacyTrait::assertTextHelper Deprecated protected function Helper for assertText and assertNoText.
AssertLegacyTrait::assertTitle Deprecated protected function Pass if the page title is the given string.
AssertLegacyTrait::assertUniqueText Deprecated protected function Passes if the text is found ONLY ONCE on the text version of the page.
AssertLegacyTrait::assertUrl Deprecated protected function Passes if the internal browser's URL matches the given path.
AssertLegacyTrait::buildXPathQuery Deprecated protected function Builds an XPath query.
AssertLegacyTrait::constructFieldXpath Deprecated protected function Helper: Constructs an XPath for the given set of attributes and value.
AssertLegacyTrait::getAllOptions Deprecated protected function Get all option elements, including nested options, in a select.
AssertLegacyTrait::getRawContent Deprecated protected function Gets the current raw content.
AssertLegacyTrait::pass Deprecated protected function
AssertLegacyTrait::verbose Deprecated protected function
PhpUnitWarnings::$deprecationWarnings private static property Deprecation warnings from PHPUnit to raise with @trigger_error().
PhpUnitWarnings::addWarning public function Converts PHPUnit deprecation warnings to E_USER_DEPRECATED.
ToolkitTestBase::$file protected property The URI for the file.
ToolkitTestBase::$image protected property The image object for the test file.
ToolkitTestBase::$imageFactory protected property The image factory service.
ToolkitTestBase::$modules protected static property Modules to enable.
ToolkitTestBase::assertToolkitOperationsCalled public function Tests that only allowed image toolkit operations are called.
ToolkitTestBase::getImage protected function Sets up an image with the custom toolkit.
ToolkitTestBase::imageTestGetAllCalls protected function Gets an array of calls to the test toolkit.
ToolkitTestBase::imageTestReset protected function Resets/initializes the history of calls to the test toolkit functions.
ToolkitTestBase::setUp protected function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.