View source  
  <?php
namespace Drupal\Tests\inline_entity_form\FunctionalJavascript;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
abstract class InlineEntityFormTestBase extends WebDriverTestBase {
  
  protected $user;
  
  protected $fieldStorageConfigStorage;
  
  protected $fieldConfigStorage;
  
  protected $defaultTheme = 'stark';
  
  protected function setUp() : void {
    parent::setUp();
    $this->fieldStorageConfigStorage = $this->container
      ->get('entity_type.manager')
      ->getStorage('field_storage_config');
    $this->fieldConfigStorage = $this->container
      ->get('entity_type.manager')
      ->getStorage('field_config');
  }
  
  protected function prepareSettings() {
    $drupal_version = (double) substr(\Drupal::VERSION, 0, 3);
    if ($drupal_version < 8.800000000000001) {
      
      $this->strictConfigSchema = FALSE;
    }
    parent::prepareSettings();
  }
  
  protected function getButtonName(string $xpath) {
    $retval = '';
    
    if ($elements = $this
      ->xpath($xpath)) {
      foreach ($elements[0]
        ->attributes() as $name => $value) {
        if ($name === 'name') {
          $retval = $value;
          break;
        }
      }
    }
    return $retval;
  }
  
  protected function assertNoNodeByTitle(string $title, $message = '') {
    if (!$message) {
      $message = "No node with title: {$title}";
    }
    $node = $this
      ->getNodeByTitle($title, TRUE);
    $this
      ->assertEmpty($node, $message);
  }
  
  protected function assertNodeByTitle(string $title, $content_type = NULL, $message = '') {
    if (!$message) {
      $message = "Node with title found: {$title}";
    }
    $node = $this
      ->getNodeByTitle($title, TRUE);
    $this
      ->assertNotEmpty($node, $message);
    if ($content_type) {
      $this
        ->assertEquals($node
        ->bundle(), $content_type, "Node is correct content type: {$content_type}");
    }
  }
  
  protected function assertEntityByLabel(string $label, $entity_type_id = 'node', $bundle = NULL) {
    $entity_type_manager = \Drupal::entityTypeManager();
    $entity_type = $entity_type_manager
      ->getDefinition($entity_type_id);
    $label_key = $entity_type
      ->getKey('label');
    $bundle_key = $entity_type
      ->getKey('bundle');
    $query = $entity_type_manager
      ->getStorage($entity_type_id)
      ->getQuery();
    $query
      ->condition($label_key, $label);
    if ($bundle && $bundle_key) {
      $query
        ->condition($bundle_key, $bundle);
    }
    $result = $query
      ->execute();
    $this
      ->assertNotEmpty($result);
  }
  
  protected function checkFormDisplayFields(string $form_display, string $prefix) {
    $assert_session = $this
      ->assertSession();
    $form_display_fields = [
      'node.ief_test_custom.default' => [
        'expected' => [
          '[title][0][value]',
          '[uid][0][target_id]',
          '[created][0][value][date]',
          '[created][0][value][time]',
          '[promote][value]',
          '[sticky][value]',
          '[positive_int][0][value]',
        ],
        'unexpected' => [],
      ],
      'node.ief_test_custom.inline' => [
        'expected' => [
          '[title][0][value]',
          '[positive_int][0][value]',
        ],
        'unexpected' => [
          '[uid][0][target_id]',
          '[created][0][value][date]',
          '[created][0][value][time]',
          '[promote][value]',
          '[sticky][value]',
        ],
      ],
    ];
    if (empty($form_display_fields[$form_display])) {
      throw new \Exception('Form display not found: ' . $form_display);
    }
    $fields = $form_display_fields[$form_display];
    foreach ($fields['expected'] as $expected_field) {
      $assert_session
        ->fieldExists($prefix . $expected_field);
    }
    foreach ($fields['unexpected'] as $unexpected_field) {
      $assert_session
        ->fieldNotExists($prefix . $unexpected_field);
    }
  }
  
  protected function waitForRowByTitle(string $title) {
    $this
      ->assertNotEmpty($this
      ->assertSession()
      ->waitForElement('xpath', '//td[@class="inline-entity-form-node-label" and text()="' . $title . '"]'));
  }
  
  protected function waitForRowRemovedByTitle(string $title) {
    $this
      ->assertNotEmpty($this
      ->assertSession()
      ->waitForElementRemoved('xpath', '//td[@class="inline-entity-form-node-label" and text()="' . $title . '"]'));
  }
  
  protected function assertRowByTitle(string $title) {
    $this
      ->assertNotEmpty($element = $this
      ->assertSession()
      ->elementExists('xpath', '//td[@class="inline-entity-form-node-label" and text()="' . $title . '"]'));
    return $element;
  }
  
  protected function assertNoRowByTitle(string $title) {
    $this
      ->assertSession()
      ->elementNotExists('xpath', '//td[@class="inline-entity-form-node-label" and text()="' . $title . '"]');
  }
  
  protected function getXpathForNthInputByLabelText(string $label, int $index) {
    return "(//*[@id=string((//label[.='{$label}']/@for)[{$index}])])";
  }
  
  protected function getXpathForAutoCompleteInput() {
    return '(//input[@data-autocomplete-path])';
  }
  
  protected function getXpathForButtonWithValue(string $value, int $index) {
    return "(//input[@type='submit' and @value='{$value}'])[{$index}]";
  }
  
  protected function getXpathForFieldsetLabel(string $label, int $index) {
    return "(//fieldset/legend/span[.='{$label}'])[{$index}]";
  }
}