View source
<?php
namespace Drupal\Tests\xmlsitemap\Kernel;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\user\Traits\UserCreationTrait;
use Drupal\user\Entity\Role;
class MetatagNoIndexTest extends KernelTestBase {
use UserCreationTrait;
protected $linkStorage;
protected $account;
public static $modules = [
'field',
'user',
'token',
'metatag',
];
public function setUp() {
parent::setUp();
$this
->installConfig([
'system',
'user',
'field',
'metatag',
]);
$this
->installEntitySchema('user');
$this
->installSchema('system', [
'sequences',
]);
$role = Role::load(AccountInterface::ANONYMOUS_ROLE);
$role
->grantPermission('access user profiles');
$role
->save();
xmlsitemap_link_bundle_enable('user', 'user');
xmlsitemap_link_bundle_settings_save('user', 'user', [
'status' => 1,
'priority' => XMLSITEMAP_PRIORITY_DEFAULT,
]);
FieldStorageConfig::create([
'entity_type' => 'user',
'field_name' => 'field_metatags',
'type' => 'metatag',
])
->save();
FieldConfig::create([
'entity_type' => 'user',
'field_name' => 'field_metatags',
'bundle' => 'user',
])
->save();
$this->linkStorage = $this->container
->get('xmlsitemap.link_storage');
$this->account = $this
->createUser();
$link = $this->linkStorage
->load('user', $this->account
->id());
$this
->assertTrue($link['access'] && $link['status']);
}
public function testEntityNoIndex() {
$this->account
->set('field_metatags', serialize([
'robots' => 'noindex',
]));
$this->account
->save();
$link = $this->linkStorage
->load('user', $this->account
->id());
$this
->assertFalse($link['access'] && $link['status']);
$this
->config('xmlsitemap.settings')
->set('metatag_exclude_noindex', FALSE)
->save(TRUE);
drupal_static_reset('metatag_xmlsitemap_link_alter');
$this->account
->save();
$link = $this->linkStorage
->load('user', $this->account
->id());
$this
->assertTrue($link['access'] && $link['status']);
}
public function testDefaultsNoIndex() {
$config = $this
->config('metatag.metatag_defaults.user');
$config
->set('tags.robots', 'noindex');
$config
->save();
$this->account
->save();
$link = $this->linkStorage
->load('user', $this->account
->id());
$this
->assertTrue($link['access'] && $link['status']);
}
}