View source
<?php
namespace Drupal\Tests\views_base_url\Functional;
use Drupal\Component\Utility\Random;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\TestFileCreationTrait;
class ViewsBaseUrlFieldTest extends BrowserTestBase {
use TestFileCreationTrait {
getTestFiles as drupalGetTestFiles;
}
protected $adminUser;
protected $profile = 'standard';
protected $nodeCount = 5;
protected $nodes;
protected $pathAliasStorage;
protected $pathAliasManager;
public static $modules = [
'views_base_url_test',
];
protected $fileSystem;
public function setUp() {
parent::setUp();
$this->adminUser = $this
->drupalCreateUser([
'create article content',
]);
$random = new Random();
$this->pathAliasStorage = $this->container
->get('entity_type.manager')
->getStorage('path_alias');
$this->pathAliasManager = $this->container
->get('path_alias.manager');
$this->fileSystem = $this->container
->get('file_system');
$this
->drupalLogin($this->adminUser);
for ($i = 1; $i <= $this->nodeCount; $i++) {
$title = $random
->name();
$image = current($this
->drupalGetTestFiles('image'));
$edit = [
'title[0][value]' => $title,
'files[field_image_0]' => $this->fileSystem
->realpath($image->uri),
];
$this
->drupalPostForm('node/add/article', $edit, t('Save'));
$this
->drupalPostForm(NULL, [
'field_image[0][alt]' => $title,
], t('Save'));
$this->nodes[$i] = $this
->drupalGetNodeByTitle($title);
$path_alias = $this->pathAliasStorage
->create([
'path' => '/node/' . $this->nodes[$i]
->id(),
'alias' => "/content/" . $title,
]);
$path_alias
->save();
}
$this
->drupalLogout();
}
protected function assertViewsBaseUrlLinkNoSettings() {
global $base_url;
$this
->drupalGet('views-base-url-link-no-settings-test');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[contains(@class,"view-views-base-url-link-no-settings-test")]/div[@class="view-content"]/div[contains(@class,"views-row")]');
$this
->assertEqual(count($elements), $this->nodeCount, t('There are @count rows', [
'@count' => $this->nodeCount,
]));
$link_path = $base_url;
$link_text = $link_path;
$elements = $this
->xpath('//a[@href=:path and text()=:text]', [
':path' => $link_path,
':text' => $link_text,
]);
$this
->assertEqual(count($elements), $this->nodeCount, 'Views base url rendered as link with no settings set');
}
public function testViewsBaseUrlNoLink() {
global $base_url;
$this
->drupalGet('views-base-url-nolink-test');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[contains(@class,"view-views-base-url-no-link-test")]/div[@class="view-content"]/div[contains(@class,"views-row")]');
$this
->assertEqual(count($elements), $this->nodeCount, t('There are @count rows', [
'@count' => $this->nodeCount,
]));
$elements = $this
->xpath('//div[contains(@class,"views-field-base-url")]/span[@class="field-content" and text()=:value]', [
':value' => $base_url,
]);
$this
->assertEqual(count($elements), $this->nodeCount, t('Base url is displayed @count times', [
'@count' => $this->nodeCount,
]));
}
public function testViewsBaseUrlLinkAllSettings() {
global $base_url;
$this
->drupalGet('views-base-url-link-all-settings-test');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[contains(@class,"view-views-base-url-link-all-settings-test")]/div[@class="view-content"]/div[contains(@class,"views-row")]');
$this
->assertEqual(count($elements), $this->nodeCount, t('There are @count rows', [
'@count' => $this->nodeCount,
]));
foreach ($this->nodes as $node) {
$link_class = 'views-base-url-test';
$link_title = $node
->getTitle();
$link_text = $node
->getTitle();
$link_rel = 'rel-attribute';
$link_target = '_blank';
$link_path = Url::fromUri($base_url . $this->pathAliasManager
->getAliasByPath('/node/' . $node
->id()), [
'attributes' => [
'class' => $link_class,
'title' => $link_title,
'rel' => $link_rel,
'target' => $link_target,
],
'fragment' => 'new',
'query' => [
'destination' => 'node',
],
])
->toUriString();
$elements = $this
->xpath('//a[@href=:path and @class=:class and @title=:title and @rel=:rel and @target=:target and text()=:text]', [
':path' => $link_path,
':class' => $link_class,
':title' => $link_title,
':rel' => $link_rel,
':target' => $link_target,
':text' => $link_text,
]);
$this
->assertEqual(count($elements), 1, 'Views base url rendered as link with all settings');
}
}
public function testViewsBaseUrlLinkLinkPath() {
global $base_url;
$this
->drupalGet('views-base-url-link-link-path-test');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[contains(@class,"view-views-base-url-link-link-path-test")]/div[@class="view-content"]/div[contains(@class,"views-row")]');
$this
->assertEqual(count($elements), $this->nodeCount, t('There are @count rows', [
'@count' => $this->nodeCount,
]));
foreach ($this->nodes as $node) {
$link_path = Url::fromUri($base_url . $this->pathAliasManager
->getAliasByPath('/node/' . $node
->id()))
->toUriString();
$link_text = $link_path;
$elements = $this
->xpath('//a[@href=:path and text()=:text]', [
':path' => $link_path,
':text' => $link_text,
]);
$this
->assertEqual(count($elements), 1, 'Views base url rendered as link with link path set');
}
}
public function testViewsBaseUrlLinkNoLinkPath() {
$this
->assertViewsBaseUrlLinkNoSettings();
}
public function testViewsBaseUrlLinkLinkText() {
global $base_url;
$this
->drupalGet('views-base-url-link-link-text-test');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[contains(@class,"view-views-base-url-link-link-text-test")]/div[@class="view-content"]/div[contains(@class,"views-row")]');
$this
->assertEqual(count($elements), $this->nodeCount, t('There are @count rows', [
'@count' => $this->nodeCount,
]));
foreach ($this->nodes as $node) {
$link_path = $base_url;
$link_text = $node
->getTitle();
$elements = $this
->xpath('//a[@href=:path and text()=:text]', [
':path' => $link_path,
':text' => $link_text,
]);
$this
->assertEqual(count($elements), 1, 'Views base url rendered as link with link text set');
}
}
public function testViewsBaseUrlLinkNoLinkText() {
$this
->assertViewsBaseUrlLinkNoSettings();
}
public function testViewsBaseUrlLinkLinkQuery() {
global $base_url;
$this
->drupalGet('views-base-url-link-link-query-test');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[contains(@class,"view-views-base-url-link-link-query-test")]/div[@class="view-content"]/div[contains(@class,"views-row")]');
$this
->assertEqual(count($elements), $this->nodeCount, t('There are @count rows', [
'@count' => $this->nodeCount,
]));
$link_path = $base_url;
$link_text = $link_path;
$elements = $this
->xpath('//a[@href=:path and text()=:text]', [
':path' => Url::fromUri($link_path, [
'query' => [
'destination' => 'node',
],
])
->toUriString(),
':text' => $link_text,
]);
$this
->assertEqual(count($elements), $this->nodeCount, 'Views base url rendered as link with link query set');
}
public function testViewsBaseUrlLinkNoLinkQuery() {
$this
->assertViewsBaseUrlLinkNoSettings();
}
public function testViewsBaseUrlImage() {
global $base_url;
$this
->drupalGet('views-base-url-image-test');
$this
->assertResponse(200);
$elements = $this
->xpath('//div[contains(@class,"view-views-base-url-image-test")]/div[@class="view-content"]/div[contains(@class,"views-row")]');
$this
->assertEqual(count($elements), $this->nodeCount, t('There are @count rows', [
'@count' => $this->nodeCount,
]));
foreach ($this->nodes as $node) {
$field = $node
->get('field_image');
$value = $field
->getValue();
$image_uri = file_url_transform_relative(file_create_url($field->entity
->getFileUri()));
$image_alt = $value[0]['alt'];
$image_width = $value[0]['width'];
$image_height = $value[0]['height'];
$link_class = 'views-base-url-test';
$link_title = $node
->getTitle();
$link_rel = 'rel-attribute';
$link_target = '_blank';
$link_path = Url::fromUri($base_url . $this->pathAliasManager
->getAliasByPath('/node/' . $node
->id()), [
'attributes' => [
'class' => $link_class,
'title' => $link_title,
'rel' => $link_rel,
'target' => $link_target,
],
'fragment' => 'new',
'query' => [
'destination' => 'node',
],
])
->toUriString();
$elements = $this
->xpath('//a[@href=:path and @class=:class and @title=:title and @rel=:rel and @target=:target]/img[@src=:url and @width=:width and @height=:height and @alt=:alt]', [
':path' => $link_path,
':class' => $link_class,
':title' => $link_title,
':rel' => $link_rel,
':target' => $link_target,
':url' => $image_uri,
':width' => $image_width,
':height' => $image_height,
':alt' => $image_alt,
]);
$this
->assertEqual(count($elements), 1, 'Views base url rendered as link image');
}
}
}