AssertButtonsTrait.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. /**
  4. * Asserts that buttons are present on a page.
  5. */
  6. trait AssertButtonsTrait {
  7. /**
  8. * Assert method to verify the buttons in the dropdown element.
  9. *
  10. * @param array $buttons
  11. * A collection of buttons to assert for on the page.
  12. * @param bool $dropbutton
  13. * Whether to check if the buttons are in a dropbutton widget or not.
  14. */
  15. public function assertButtons(array $buttons, $dropbutton = TRUE) {
  16. // Try to find a Save button.
  17. $save_button = $this->xpath('//input[@type="submit"][@value="Save"]');
  18. // Verify that the number of buttons passed as parameters is
  19. // available in the dropbutton widget.
  20. if ($dropbutton) {
  21. $i = 0;
  22. $count = count($buttons);
  23. // Assert there is no save button.
  24. $this->assertTrue(empty($save_button));
  25. // Dropbutton elements.
  26. /** @var \Behat\Mink\Element\NodeElement[] $elements */
  27. $elements = $this->xpath('//div[@class="dropbutton-wrapper"]//input[@type="submit"]');
  28. $this->assertEqual($count, count($elements));
  29. foreach ($elements as $element) {
  30. $value = $element->getValue() ?: '';
  31. $this->assertEqual($buttons[$i], $value);
  32. $i++;
  33. }
  34. }
  35. else {
  36. // Assert there is a save button.
  37. $this->assertTrue(!empty($save_button));
  38. $this->assertNoRaw('dropbutton-wrapper');
  39. }
  40. }
  41. }