View source
<?php
namespace Drupal\Tests\lingotek\FunctionalJavascript;
use Behat\Mink\Element\NodeElement;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
class LingotekSourceActionsTest extends LingotekFunctionalJavascriptTestBase {
public static $modules = [
'system',
'block',
'node',
'lingotek_form_test',
];
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('page_title_block', [
'region' => 'content',
'weight' => -5,
]);
$this
->drupalPlaceBlock('local_tasks_block', [
'region' => 'content',
'weight' => -10,
]);
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
ConfigurableLanguage::createFromLangcode('es')
->setThirdPartySetting('lingotek', 'locale', 'es_ES')
->save();
ConfigurableLanguage::createFromLangcode('it')
->setThirdPartySetting('lingotek', 'locale', 'it_IT')
->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
$this
->saveLingotekContentTranslationSettingsForNodeTypes();
}
public function testDropdownSourceStatus() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('node/add/article');
$edit = [];
$edit['title[0][value]'] = 'Llamas are cool';
$edit['langcode[0][value]'] = 'en';
$edit['body[0][value]'] = 'Llamas are very cool';
$this
->drupalPostForm(NULL, $edit, t('Save'));
$translationService = \Drupal::service('lingotek.content_translation');
$this
->drupalGet('/lingotek_form_test/lingotek_source_status/node/1');
$buttonLocator = 'button.lingotek-source-dropdown-toggle';
$listLocator = 'ul.lingotek-source-actions';
$assert_session
->elementsCount('css', $buttonLocator, 1);
$assert_session
->elementsCount('css', $listLocator, 1);
$lists = $page
->findAll('css', $listLocator);
$visibleLists = $this
->filterVisibleElements($lists);
$this
->assertCount(0, $visibleLists);
$button_field = $assert_session
->elementExists('css', $buttonLocator);
$button_field
->click();
$listShown = $this
->waitForVisibleElementCount(1, $listLocator);
$this
->assertTrue($listShown);
$visibleLists = $this
->filterVisibleElements($lists);
$this
->assertCount(1, $visibleLists);
}
protected function filterVisibleElements(array $elements) {
$elements = array_filter($elements, function (NodeElement $element) {
return $element
->isVisible();
});
return $elements;
}
protected function waitForVisibleElementCount($count, $locator, $timeout = 10000) {
$page = $this
->getSession()
->getPage();
return $page
->waitFor($timeout / 1000, function () use ($count, $page, $locator) {
$elements = $page
->findAll('css', $locator);
$visible_elements = $this
->filterVisibleElements($elements);
if (count($visible_elements) === $count) {
return TRUE;
}
return FALSE;
});
}
}