You are here

public function WebformImageSelectImagesTest::testWebformImageSelectImages in Webform 8.5

Same name and namespace in other branches
  1. 6.x modules/webform_image_select/tests/src/Functional/WebformImageSelectImagesTest.php \Drupal\Tests\webform_image_select\Functional\WebformImageSelectImagesTest::testWebformImageSelectImages()

Tests webform image select images entity.

File

modules/webform_image_select/tests/src/Functional/WebformImageSelectImagesTest.php, line 27

Class

WebformImageSelectImagesTest
Tests for webform image select image entity.

Namespace

Drupal\Tests\webform_image_select\Functional

Code

public function testWebformImageSelectImages() {
  $normal_user = $this
    ->drupalCreateUser();
  $admin_user = $this
    ->drupalCreateUser([
    'administer webform',
  ]);

  /**************************************************************************/
  $this
    ->drupalLogin($normal_user);

  // Check get element images.
  $kittens = Yaml::decode("kitten_1:\n  text: 'Cute Kitten 1'\n  src: 'http://placekitten.com/220/200'\nkitten_2:\n  text: 'Cute Kitten 2'\n  src: 'http://placekitten.com/180/200'\nkitten_3:\n  text: 'Cute Kitten 3'\n  src: 'http://placekitten.com/130/200'\nkitten_4:\n  text: 'Cute Kitten 4'\n  src: 'http://placekitten.com/270/200'");
  $element = [
    '#images' => $kittens,
  ];
  $this
    ->assertEqual(WebformImageSelectImages::getElementImages($element), $kittens);
  $element = [
    '#images' => 'kittens',
  ];
  $this
    ->assertEqual(WebformImageSelectImages::getElementImages($element), $kittens);
  $element = [
    '#images' => 'not-found',
  ];
  $this
    ->assertEqual(WebformImageSelectImages::getElementImages($element), []);
  $dogs = Yaml::decode("dog_1:\n  text: 'Cute Dog 1'\n  src: 'http://placedog.net/220/200'\ndog_2:\n  text: 'Cute Dog 2'\n  src: 'http://placedog.net/180/200'\ndog_3:\n  text: 'Cute Dog 3'\n  src: 'http://placedog.net/130/200'\ndog_4:\n  text: 'Cute Dog 4'\n  src: 'http://placedog.net/270/200'");

  // Check get element images for manually defined images.
  $element = [
    '#images' => $dogs,
  ];
  $this
    ->assertEqual(WebformImageSelectImages::getElementImages($element), $dogs);

  /** @var \Drupal\webform_image_select\WebformImageSelectImagesInterface $webform_images */
  $webform_images = WebformImageSelectImages::create([
    'langcode' => 'en',
    'status' => WebformInterface::STATUS_OPEN,
    'id' => 'dogs',
    'title' => 'Dogs',
    'images' => Yaml::encode($dogs),
  ]);
  $webform_images
    ->save();

  // Check get images.
  $this
    ->assertEqual($webform_images
    ->getImages(), $dogs);

  // Set invalid images.
  $webform_images
    ->set('images', "not\nvalid\nyaml")
    ->save();

  // Check invalid images.
  $this
    ->assertEqual([], $webform_images
    ->getImages());

  // Check admin user access denied.
  $this
    ->drupalGet('/admin/structure/webform/config/images/manage');
  $this
    ->assertResponse(403);
  $this
    ->drupalGet('/admin/structure/webform/config/images/manage/add');
  $this
    ->assertResponse(403);
  $this
    ->drupalGet('/admin/structure/webform/config/images/manage/animals/edit');
  $this
    ->assertResponse(403);

  // Check admin user access.
  $this
    ->drupalLogin($admin_user);
  $this
    ->drupalGet('/admin/structure/webform/config/images/manage');
  $this
    ->assertResponse(200);
  $this
    ->drupalGet('/admin/structure/webform/config/images/manage/add');
  $this
    ->assertResponse(200);

  // Check image altered message.
  $this
    ->drupalGet('/admin/structure/webform/config/images/manage/animals/edit');
  $this
    ->assertRaw('The <em class="placeholder">Cute Animals</em> images are being altered by the <em class="placeholder">Webform Image Select test</em> module.');

  // Check hook_webform_image_select_images_alter().
  // Check hook_webform_image_select_images_WEBFORM_IMAGE_SELECT_IMAGES_ID_alter().
  $element = [
    '#images' => 'animals',
  ];
  $images = WebformImageSelectImages::getElementImages($element);
  $this
    ->debug($images);
  $this
    ->assertEqual(array_keys($images), [
    'kitten_1',
    'kitten_2',
    'kitten_3',
    'kitten_4',
    'dog_1',
    'dog_2',
    'dog_3',
    'dog_4',
  ]);
}