View source
<?php
namespace Drupal\Tests\business_rules\FunctionalJavascript;
use Drupal\Core\Url;
use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\VocabularyInterface;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
class DependentFieldsView extends WebDriverTestBase {
use TaxonomyTestTrait;
protected $defaultTheme = 'stark';
public static $modules = [
'business_rules_dependent_fields_view',
];
protected $sutTerms;
protected $sutUsers;
public function setUp() {
parent::setUp();
$vocab = Vocabulary::load('vocab1');
$this
->assertInstanceOf(VocabularyInterface::class, $vocab, 'Test vocabulary can be loaded.');
$this->sutTerms[] = $this
->createTerm($vocab, [
'name' => 'vocab1term1',
]);
$this
->assertInstanceOf(Term::class, $this->sutTerms[0], 'First test term can be created.');
$this->sutTerms[] = $this
->createTerm($vocab, [
'name' => 'vocab1term2',
]);
$this
->assertInstanceOf(Term::class, $this->sutTerms[1], 'Second test term can be created.');
$this->sutUsers[] = $this
->drupalCreateUser([], 'user1', FALSE, [
'field_user_term' => $this->sutTerms[0],
]);
$this->sutUsers[] = $this
->drupalCreateUser([], 'user2', FALSE, [
'field_user_term' => $this->sutTerms[1],
]);
$this->sutUsers[] = $this
->drupalCreateUser([], 'user3', FALSE, [
'field_user_term' => $this->sutTerms[0],
]);
$this->sutUsers[] = $this
->drupalCreateUser([], 'user4', FALSE, [
'field_user_term' => $this->sutTerms[1],
]);
}
public function testTriggeringFieldCanAffectTargetFieldInNode() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer nodes',
'bypass node access',
]));
$this
->drupalGet(Url::fromRoute('node.add', [
'node_type' => 'nodetype1',
]));
$page = $this
->getSession()
->getPage();
$triggeringField = $page
->find('named', [
'select',
'termfield1',
]);
$targetFieldLabel = 'user1';
$triggeringField
->selectOption($this->sutTerms[0]
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->optionExists($targetFieldLabel, $this->sutUsers[0]
->label());
$this
->assertSession()
->optionNotExists($targetFieldLabel, $this->sutUsers[1]
->label());
$this
->assertSession()
->optionExists($targetFieldLabel, $this->sutUsers[2]
->label());
$this
->assertSession()
->optionNotExists($targetFieldLabel, $this->sutUsers[3]
->label());
$triggeringField
->selectOption($this->sutTerms[1]
->id());
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->optionNotExists($targetFieldLabel, $this->sutUsers[0]
->label());
$this
->assertSession()
->optionExists($targetFieldLabel, $this->sutUsers[1]
->label());
$this
->assertSession()
->optionNotExists($targetFieldLabel, $this->sutUsers[2]
->label());
$this
->assertSession()
->optionExists($targetFieldLabel, $this->sutUsers[3]
->label());
}
}