You are here

public function ProductAdminTest::testAdminProducts in Commerce Core 8.2

Tests viewing the admin/commerce/products page.

File

modules/product/tests/src/Functional/ProductAdminTest.php, line 159

Class

ProductAdminTest
Create, view, edit, delete, and change products.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testAdminProducts() {
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextNotContains('You are not authorized to access this page.');
  $this
    ->assertNotEmpty($this
    ->getSession()
    ->getPage()
    ->hasLink('Add product'));

  // Create a default type product.
  $product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => 'First product',
    'status' => TRUE,
  ]);

  // Create a second product type and products for that type.
  $values = [
    'id' => 'random',
    'label' => 'Random',
    'description' => 'My random product type',
    'variationType' => 'default',
  ];
  $product_type = $this
    ->createEntity('commerce_product_type', $values);

  /** @var \Drupal\commerce_product\Entity\ProductInterface $second_product */
  $second_product = $this
    ->createEntity('commerce_product', [
    'type' => 'random',
    'title' => 'Second product',
    'status' => FALSE,
  ]);

  /** @var \Drupal\commerce_product\Entity\ProductInterface $third_product */
  $third_product = $this
    ->createEntity('commerce_product', [
    'type' => 'random',
    'title' => 'Third product',
    'status' => TRUE,
  ]);
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->assertSession()
    ->pageTextNotContains('You are not authorized to access this page.');
  $row_count = $this
    ->getSession()
    ->getPage()
    ->findAll('xpath', '//table/tbody/tr');
  $this
    ->assertEquals(3, count($row_count));

  // Confirm that product titles are displayed.
  $page = $this
    ->getSession()
    ->getPage();
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td/a[text()="First product"]');
  $this
    ->assertEquals(1, count($product_count), 'First product is displayed.');
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td/a[text()="Second product"]');
  $this
    ->assertEquals(1, count($product_count), 'Second product is displayed.');
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td/a[text()="Third product"]');
  $this
    ->assertEquals(1, count($product_count), 'Third product is displayed.');

  // Confirm that product types are displayed.
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td[starts-with(text(), "Default")]');
  $this
    ->assertEquals(1, count($product_count), 'Default product type exists in the table.');
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td[starts-with(text(), "Random")]');
  $this
    ->assertEquals(2, count($product_count), 'Random product types exist in the table.');

  // Confirm that product statuses are displayed.
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td[starts-with(text(), "Unpublished")]');
  $this
    ->assertEquals(1, count($product_count), 'Unpublished product exists in the table.');
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td[starts-with(text(), "Published")]');
  $this
    ->assertEquals(2, count($product_count), 'Published products exist in the table.');

  // Logout and check that anonymous users cannot see the products page
  // and receive a 403 error code.
  $this
    ->drupalLogout();
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->assertSession()
    ->pageTextContains('You are not authorized to access this page.');
  $this
    ->assertEmpty($this
    ->getSession()
    ->getPage()
    ->hasLink('Add product'));

  // Login and confirm access for 'access commerce_product overview'
  // permission. The second product should no longer be visible because
  // it is unpublished.
  $user = $this
    ->drupalCreateUser([
    'access commerce_product overview',
  ]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->assertSession()
    ->pageTextNotContains('You are not authorized to access this page.');
  $this
    ->assertEmpty($this
    ->getSession()
    ->getPage()
    ->hasLink('Add product'));
  $row_count = $this
    ->getSession()
    ->getPage()
    ->findAll('xpath', '//table/tbody/tr');
  $this
    ->assertEquals(2, count($row_count));

  // Confirm that product titles are displayed.
  $page = $this
    ->getSession()
    ->getPage();
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td/a[text()="First product"]');
  $this
    ->assertEquals(1, count($product_count), 'First product is displayed.');
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td/a[text()="Third product"]');
  $this
    ->assertEquals(1, count($product_count), 'Third product is displayed.');

  // Confirm that the right product statuses are displayed.
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td[starts-with(text(), "Unpublished")]');
  $this
    ->assertEquals(0, count($product_count), 'Unpublished product do not exist in the table.');
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td[starts-with(text(), "Published")]');
  $this
    ->assertEquals(2, count($product_count), 'Published products exist in the table.');

  // Confirm that product types are displayed.
  $this
    ->assertSession()
    ->optionExists('edit-type', 'default');
  $this
    ->assertSession()
    ->optionExists('edit-type', 'random');
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td[starts-with(text(), "Default")]');
  $this
    ->assertEquals(1, count($product_count));
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td[starts-with(text(), "Random")]');
  $this
    ->assertEquals(1, count($product_count));

  // Confirm that the product type filter respects view access.
  $authenticated_role = Role::load(RoleInterface::AUTHENTICATED_ID);
  $authenticated_role
    ->revokePermission('view commerce_product');
  $authenticated_role
    ->save();
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->assertSession()
    ->pageTextContains('No products available');
  $this
    ->assertSession()
    ->optionNotExists('edit-type', 'default');
  $this
    ->assertSession()
    ->optionNotExists('edit-type', 'random');
  $authenticated_role
    ->grantPermission('view default commerce_product');
  $authenticated_role
    ->save();
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->assertSession()
    ->optionExists('edit-type', 'default');
  $this
    ->assertSession()
    ->optionNotExists('edit-type', 'random');
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td[starts-with(text(), "Default")]');
  $this
    ->assertEquals(1, count($product_count));
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td[starts-with(text(), "Random")]');
  $this
    ->assertEquals(0, count($product_count));

  // Login and confirm access for "view own unpublished commerce_product".
  $user = $this
    ->drupalCreateUser([
    'access commerce_product overview',
    'view own unpublished commerce_product',
  ]);
  $second_product
    ->setOwnerId($user
    ->id());
  $second_product
    ->save();
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('admin/commerce/products');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->assertSession()
    ->pageTextNotContains('You are not authorized to access this page.');
  $product_count = $page
    ->findAll('xpath', '//table/tbody/tr/td/a[text()="Second product"]');
  $this
    ->assertEquals(1, count($product_count), 'Second product is displayed.');
}