You are here

public function WishlistItemAccessTest::testAccess in Commerce Wishlist 8.3

@covers ::checkAccess

File

tests/src/Kernel/WishlistItemAccessTest.php, line 39

Class

WishlistItemAccessTest
Tests the wishlist item access control.

Namespace

Drupal\Tests\commerce_wishlist\Kernel

Code

public function testAccess() {

  /** @var \Drupal\commerce_product\Entity\ProductVariationInterface $variation */
  $variation = ProductVariation::create([
    'type' => 'default',
    'sku' => $this
      ->randomMachineName(),
    'title' => $this
      ->randomString(),
    'status' => 1,
  ]);
  $variation
    ->save();

  /** @var \Drupal\commerce_wishlist\Entity\WishlistItemInterface $wishlist_item */
  $wishlist_item = WishlistItem::create([
    'type' => 'commerce_product_variation',
    'purchasable_entity' => $variation,
  ]);
  $wishlist_item
    ->save();
  $wishlist = Wishlist::create([
    'type' => 'default',
    'name' => 'My wishlist',
    'wishlist_items' => [
      $wishlist_item,
    ],
  ]);
  $wishlist
    ->save();
  $wishlist_item = $this
    ->reloadEntity($wishlist_item);
  $account = $this
    ->createUser([], [
    'access administration pages',
  ]);
  $this
    ->assertFalse($wishlist_item
    ->access('view', $account));
  $this
    ->assertFalse($wishlist_item
    ->access('update', $account));
  $this
    ->assertFalse($wishlist_item
    ->access('delete', $account));
  $account = $this
    ->createUser([], [
    'view any commerce_wishlist',
  ]);
  $this
    ->assertTrue($wishlist_item
    ->access('view', $account));
  $this
    ->assertFalse($wishlist_item
    ->access('update', $account));
  $this
    ->assertFalse($wishlist_item
    ->access('delete', $account));
  $account = $this
    ->createUser([], [
    'update any default commerce_wishlist',
  ]);
  $this
    ->assertFalse($wishlist_item
    ->access('view', $account));
  $this
    ->assertFalse($wishlist_item
    ->access('update', $account));
  $this
    ->assertFalse($wishlist_item
    ->access('delete', $account));
  $account = $this
    ->createUser([], [
    'manage commerce_product_variation commerce_wishlist_item',
  ]);
  $this
    ->assertFalse($wishlist_item
    ->access('view', $account));
  $this
    ->assertTrue($wishlist_item
    ->access('update', $account));
  $this
    ->assertTrue($wishlist_item
    ->access('delete', $account));
  $account = $this
    ->createUser([], [
    'administer commerce_wishlist',
  ]);
  $this
    ->assertTrue($wishlist_item
    ->access('view', $account));
  $this
    ->assertTrue($wishlist_item
    ->access('update', $account));
  $this
    ->assertTrue($wishlist_item
    ->access('delete', $account));

  // Broken wishlist reference.
  $wishlist_item
    ->set('wishlist_id', '999');
  $account = $this
    ->createUser([], [
    'manage commerce_product_variation commerce_wishlist_item',
  ]);
  $this
    ->assertFalse($wishlist_item
    ->access('view', $account));
  $this
    ->assertFalse($wishlist_item
    ->access('update', $account));
  $this
    ->assertFalse($wishlist_item
    ->access('delete', $account));
  $account = $this
    ->createUser([], [
    'view own commerce_wishlist',
  ]);
  $wishlist
    ->setOwnerId($account
    ->id());
  $wishlist
    ->save();
  $wishlist_item = $this
    ->reloadEntity($wishlist_item);
  $this
    ->assertTrue($wishlist_item
    ->access('view', $account));
  $this
    ->assertFalse($wishlist_item
    ->access('update', $account));
  $this
    ->assertFalse($wishlist_item
    ->access('delete', $account));
}