View source
<?php
namespace Drupal\Tests\domain_source\Functional;
use Drupal\Tests\domain\Functional\DomainTestBase;
class DomainSourceEntityReferenceTest extends DomainTestBase {
public static $modules = [
'domain',
'domain_source',
'field',
'field_ui',
'menu_ui',
'block',
];
public function testDomainSourceNodeField() {
$this->admin_user = $this
->drupalCreateUser([
'administer content types',
'administer node fields',
'administer node display',
'administer domains',
]);
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('admin/structure/types/manage/article/fields');
$this
->assertResponse(200, 'Manage fields page sourceed.');
$this
->assertText('Domain Source', 'Domain form field found.');
$this
->drupalGet('admin/structure/types/manage/article/display');
$this
->assertResponse(200, 'Manage field display page sourceed.');
$this
->assertText('Domain Source', 'Domain form field found.');
}
public function testDomainSourceFieldStorage() {
$this->admin_user = $this
->drupalCreateUser([
'administer content types',
'administer node fields',
'administer node display',
'administer domains',
'administer menu',
]);
$this
->drupalLogin($this->admin_user);
$this
->domainCreateTestDomains(5);
$this
->drupalGet('node/add/article');
$this
->assertResponse(200);
$this
->assertText('Domain Source', 'Found the domain field instance.');
$domains = \Drupal::entityTypeManager()
->getStorage('domain')
->loadMultiple();
foreach ($domains as $domain) {
$string = 'value="' . $domain
->id() . '"';
$this
->assertRaw($string, 'Found the domain option.');
if (!isset($one)) {
$one = $domain
->id();
continue;
}
if (!isset($two)) {
$two = $domain
->id();
$two_path = $domain
->getPath();
}
}
$this
->assertRaw('value="_none"', 'Found the _none_ option.');
$node_storage = \Drupal::entityTypeManager()
->getStorage('node');
$edit['title[0][value]'] = 'Test node';
$edit['field_domain_source'] = $two;
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$this
->assertResponse(200);
$node = $node_storage
->load(1);
$value = domain_source_get($node);
$this
->assertEquals($two, $value, 'Node saved with proper source record.');
$url = $node
->toUrl()
->toString();
$expected_url = $two_path . 'node/1';
$this
->assertEquals($expected_url, $url, 'URL rewritten correctly.');
$edit['title[0][value]'] = 'Test node';
$edit["field_domain_source"] = '_none';
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$this
->assertResponse(200);
$node = $node_storage
->load(2);
$value = domain_source_get($node);
$this
->assertNull($value, 'Node saved with proper source record.');
$url = $node
->toUrl()
->toString();
$expected_url = base_path() . 'node/2';
$this
->assertEquals($expected_url, $url, 'URL rewritten correctly.');
$this
->drupalPlaceBlock('system_menu_block:main');
$edit = [
'menu_options[main]' => 1,
'menu_parent' => 'main:',
];
$this
->drupalGet('admin/structure/types/manage/article');
$this
->submitForm($edit, 'Save content type');
$edit = [
'title[0][value]' => 'Node 3',
'menu[enabled]' => 1,
'menu[title]' => 'Test preview',
'field_domain_source' => $two,
];
$this
->drupalGet('node/add/article');
$this
->submitForm($edit, 'Save');
$node = $node_storage
->load(3);
$url = $node
->toUrl()
->toString();
$expected_url = $two_path . 'node/3';
$this
->assertEquals($expected_url, $url, 'URL rewritten correctly.');
$this
->drupalGet('node/3');
$this
->assertRaw('href="' . $url, 'Menu link rewritten correctly.');
$id = 'node.article.field_domain_source';
if ($field = \Drupal::entityTypeManager()
->getStorage('field_config')
->load($id)) {
$field
->delete();
field_purge_batch(10, $field
->uuid());
drupal_flush_all_caches();
}
$this
->drupalGet('node/add/article');
$this
->assertResponse(200);
$edit2['title[0][value]'] = 'Test node';
$this
->drupalGet('node/add/article');
$this
->submitForm($edit2, 'Save');
$node = $node_storage
->load(4);
$url = $node
->toUrl()
->toString();
$expected_url = base_path() . 'node/4';
$this
->assertEquals($expected_url, $url, 'No URL rewrite performed.');
}
}