View source
<?php
namespace Drupal\Tests\tour_ui\Functional;
use Drupal\Tests\BrowserTestBase;
class TourUITest extends BrowserTestBase {
public static $modules = [
'tour_ui',
'tour_test',
];
protected $defaultTheme = 'stark';
public static function getInfo() {
return [
'name' => 'Tour UI',
'description' => 'Tests the Tour UI.',
'group' => 'Tour',
];
}
public function testUi() {
$this
->drupalLogin($this
->drupalCreateUser([
'administer tour',
]));
$this
->listTest();
$this
->editTest();
$this
->tipTest();
}
protected function listTest() {
$this
->drupalGet('admin/config/user-interface/tour');
$elements = $this
->xpath('//table/tbody/tr');
$this
->assertEquals(5, count($elements));
$elements = $this
->xpath('//table/tbody/tr[contains(@class, :class)]/td[1]', [
':class' => 'tour-test',
]);
$this
->assertIdentical($elements[0]
->getText(), 'tour-test');
$elements = $this
->xpath('//table/tbody/tr[contains(@class, :class)]/td[2]', [
':class' => 'tour-test',
]);
$this
->assertIdentical($elements[0]
->getText(), t('Tour test english')
->render());
$elements = $this
->xpath('//table/tbody/tr[contains(@class, :class)]/td[4]', [
':class' => 'tour-test',
]);
$this
->assertIdentical($elements[0]
->getText(), '1', 'Core tour_test/config/tour-test-2 has 1 tip');
$this
->assertIdentical($elements[1]
->getText(), '3', 'Core tour_test/config/tour-test-1 has 3 tips');
}
protected function editTest() {
$edit = [
'label' => 'a' . $this
->randomString(),
'id' => strtolower($this
->randomMachineName()),
'module' => strtolower($this
->randomMachineName()),
];
$this
->drupalPostForm('admin/config/user-interface/tour/add', $edit, t('Save'));
$this
->assertRaw(t('The %tour tour has been created.', [
'%tour' => $edit['label'],
]));
$elements = $this
->xpath('//table/tbody/tr');
$this
->assertEquals(1, count($elements));
$this
->assertTitle(t('Edit tour | @site-name', [
'@site-name' => \Drupal::config('system.site')
->get('name'),
]));
$this
->drupalPostForm(NULL, [], t('Save'));
$this
->assertRaw(t('Updated the %tour tour', [
'%tour' => $edit['label'],
]));
$this
->drupalGet('admin/config/user-interface/tour/manage/tour-test');
$weights = [
'tips[tour-test-1][weight]' => '2',
'tips[tour-test-3][weight]' => '1',
];
$this
->drupalPostForm(NULL, $weights, t('Save'));
$this
->drupalGet('admin/config/user-interface/tour/manage/tour-test');
$elements = $this
->xpath('//tr[@class=:class and ./td[contains(., :text)]]', [
':class' => 'draggable odd',
':text' => 'The awesome image',
]);
$this
->assertEquals(1, count($elements), 'Found odd tip "The awesome image".');
$elements = $this
->xpath('//tr[@class=:class and ./td[contains(., :text)]]', [
':class' => 'draggable even',
':text' => 'The first tip',
]);
$this
->assertEquals(1, count($elements), 'Found even tip "The first tip".');
$weights = [
'tips[tour-test-1][weight]' => '1',
'tips[tour-test-3][weight]' => '2',
];
$this
->drupalPostForm(NULL, $weights, t('Save'));
$this
->drupalGet('admin/config/user-interface/tour/manage/tour-test');
$elements = $this
->xpath('//tr[@class=:class and ./td[contains(., :text)]]', [
':class' => 'draggable odd',
':text' => 'The first tip',
]);
$this
->assertEquals(1, count($elements), 'Found odd tip "The first tip".');
$elements = $this
->xpath('//tr[@class=:class and ./td[contains(., :text)]]', [
':class' => 'draggable even',
':text' => 'The awesome image',
]);
$this
->assertEquals(1, count($elements), 'Found even tip "The awesome image".');
$this
->drupalPostForm('admin/config/user-interface/tour/add', $edit, t('Save'));
$this
->assertRaw(t('The machine-readable name is already in use. It must be unique.'));
$this
->drupalGet('admin/config/user-interface/tour/manage/' . $edit['id']);
$this
->drupalPostForm(NULL, NULL, t('Delete'));
$this
->assertRaw(t('Are you sure you want to delete the %tour tour?', [
'%tour' => $edit['label'],
]));
$this
->clickLink(t('Cancel'));
$this
->clickLink(t('Delete'));
$this
->drupalPostForm(NULL, NULL, t('Delete'));
$elements = $this
->xpath('//table/tbody/tr');
$this
->assertEquals(2, count($elements));
$this
->assertRaw(t('Deleted the %tour tour.', [
'%tour' => $edit['label'],
]));
}
protected function tipTest() {
$edit = [
'label' => 'a' . $this
->randomString(),
'id' => strtolower($this
->randomMachineName()),
'module' => $this
->randomString(),
'paths' => '',
];
$this
->drupalPostForm('admin/config/user-interface/tour/add', $edit, t('Save'));
$this
->assertRaw(t('The %tour tour has been created.', [
'%tour' => $edit['label'],
]));
$tip = [
'new' => 'image',
];
$this
->drupalPostForm('admin/config/user-interface/tour/manage/' . $edit['id'], $tip, t('Add'));
$tip = [
'label' => 'a' . $this
->randomString(),
'id' => 'tour-ui-test-image-tip',
'url' => 'http://testimage.png',
'alt' => 'Testing a new image tip through Tour UI.',
];
$this
->drupalPostForm(NULL, $tip, t('Save'));
$elements = $this
->xpath('//tr[@class=:class and ./td[contains(., :text)]]', [
':class' => 'draggable odd',
':text' => $tip['label'],
]);
$this
->assertEquals(1, count($elements), 'Found tip "' . $tip['label'] . '".');
$tip_id = $tip['id'];
unset($tip['id']);
$tip['label'] = 'a' . $this
->randomString();
$this
->drupalPostForm('admin/config/user-interface/tour/manage/' . $edit['id'] . '/tip/edit/' . $tip_id, $tip, t('Save'));
$elements = $this
->xpath('//tr[@class=:class and ./td[contains(., :text)]]', [
':class' => 'draggable odd',
':text' => $tip['label'],
]);
$this
->assertEquals(1, count($elements), 'Found tip "' . $tip['label'] . '".');
$this
->drupalPostForm('admin/config/user-interface/tour/manage/' . $edit['id'] . '/tip/delete/' . $tip_id, [], t('Delete'));
$elements = $this
->xpath('//tr[@class=:class and ./td[contains(., :text)]]', [
':class' => 'draggable odd',
':text' => $tip['label'],
]);
$this
->assertNotEqual(count($elements), 1, 'Did not find tip "' . $tip['label'] . '".');
}
}