ispim-1.0.x-dev/tests/src/FunctionalJavascript/IspimPreviewImageCrudTest.php

tests/src/FunctionalJavascript/IspimPreviewImageCrudTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\ispim\FunctionalJavascript;

use Behat\Mink\Element\DocumentElement;

class IspimPreviewImageCrudTest extends TestBase {

  /**
   * @throws \Throwable
   */
  public function testCrud(): void {
    $admin = $this->createUser(
      [
        'access administration pages',
        'view the administration theme',
        'ispim.ispim_preview_image._all',
      ],
    );

    $this->drupalLogin($admin);

    $values = [
      'id' => 1,
      'image' => \Drupal::root() . '/core/modules/image/sample.png',
      'label' => 'My Image 01',
      'weight' => 4,
    ];
    $this->crudCreate($values);

    $old = $values;
    $values['label'] .= ' - edit 01';
    unset($values['image']);
    $this->crudEdit($old, $values);

    $this->crudDelete($values);
  }

  /**
   * @phpstan-param array<string, mixed> $values
   *
   * @throws \Behat\Mink\Exception\ElementNotFoundException
   * @throws \Behat\Mink\Exception\ResponseTextException
   */
  protected function crudCreate(array $values): static {
    $this->drupalGet('/admin/config/media/ispim-preview-image');
    $this->clickLink('new Image Style Preview Image');

    $page = $this->getSession()->getPage();
    $this->fillIspimPreviewImageForm($page, $values, 'add');
    $page->pressButton('Save');

    $assert = $this->assertSession();
    $assert->pageTextContains(sprintf(
      '"%s" Image Style Preview Image has been created',
      $values['label'],
    ));

    return $this;
  }

  /**
   * @phpstan-param array<string, mixed> $old
   * @phpstan-param array<string, mixed> $new
   *
   * @throws \Behat\Mink\Exception\ExpectationException
   * @throws \Behat\Mink\Exception\ElementNotFoundException
   * @throws \Behat\Mink\Exception\ResponseTextException
   */
  protected function crudEdit(array $old, array $new): static {
    $this->drupalGet("/admin/config/media/ispim-preview-image/manage/{$new['id']}/edit");

    $assert = $this->assertSession();
    $assert->fieldValueEquals('label[0][value]', $old['label']);
    $assert->fieldValueEquals('weight[0][value]', (string) $old['weight']);

    $page = $this->getSession()->getPage();
    $this->fillIspimPreviewImageForm($page, $new, 'edit');
    $page->pressButton('Save');

    $assert = $this->assertSession();
    $assert->pageTextContains(sprintf(
      '"%s" Image Style Preview Image has been updated',
      $new['label'],
    ));

    return $this;
  }

  /**
   * @phpstan-param array<string, mixed> $values
   *
   * @throws \Behat\Mink\Exception\ElementNotFoundException
   * @throws \Behat\Mink\Exception\ResponseTextException
   */
  protected function crudDelete(array $values): static {
    $this->drupalGet("/admin/config/media/ispim-preview-image/manage/{$values['id']}/delete");

    $assert = $this->assertSession();
    $assert->pageTextContains(sprintf(
      'Are you sure you want to delete the Image Style Preview Image %s?',
      $values['label'],
    ));

    $this
      ->getSession()
      ->getPage()
      ->pressButton('Delete');

    $assert = $this->assertSession();
    $assert->pageTextContains(sprintf(
      'The Image Style Preview Image %s has been deleted.',
      $values['label'],
    ));

    return $this;
  }

  /**
   * @phpstan-param array<string, mixed> $values
   *
   * @throws \Behat\Mink\Exception\ElementNotFoundException
   */
  protected function fillIspimPreviewImageForm(DocumentElement $page, array $values, string $type): static {
    if (array_key_exists('image', $values)) {
      $page->attachFileToField('files[image_0]', $values['image']);
    }

    if (array_key_exists('label', $values)) {
      $page->fillField('label[0][value]', $values['label']);
    }

    if (array_key_exists('weight', $values)) {
      $page->fillField('weight[0][value]', (string) $values['weight']);
    }

    return $this;
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc