Newer
Older
Lee Rowlands
committed
namespace Drupal\Tests\taxonomy\Functional\Views;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
Alex Pott
committed
use Drupal\field\Tests\EntityReference\EntityReferenceTestTrait;
Lee Rowlands
committed
use Drupal\Tests\views\Functional\ViewTestBase;
catch
committed
use Drupal\views\Tests\ViewTestData;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term;
/**
* Base class for all taxonomy tests.
*/
abstract class TaxonomyTestBase extends ViewTestBase {
Angie Byron
committed
use EntityReferenceTestTrait;
/**
* Modules to enable.
*
* @var array
*/
public static $modules = ['taxonomy', 'taxonomy_test_views'];
/**
* Stores the nodes used for the different tests.
*
* @var \Drupal\node\NodeInterface[]
protected $nodes = [];
/**
* The vocabulary used for creating terms.
*
* @var \Drupal\taxonomy\VocabularyInterface
*/
protected $vocabulary;
/**
* Stores the first term used in the different tests.
*
* @var \Drupal\taxonomy\TermInterface
*/
protected $term1;
/**
* Stores the second term used in the different tests.
*
* @var \Drupal\taxonomy\TermInterface
*/
protected $term2;
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE) {
parent::setUp($import_test_views);
$this->mockStandardInstall();
if ($import_test_views) {
ViewTestData::createTestViews(get_class($this), ['taxonomy_test_views']);
catch
committed
$this->term1 = $this->createTerm();
$this->term2 = $this->createTerm();
$node = [];
$node['type'] = 'article';
Dries Buytaert
committed
$node['field_views_testing_tags'][]['target_id'] = $this->term1->id();
$node['field_views_testing_tags'][]['target_id'] = $this->term2->id();
$this->nodes[] = $this->drupalCreateNode($node);
$this->nodes[] = $this->drupalCreateNode($node);
}
/**
* Provides a workaround for the inability to use the standard profile.
*
Jess
committed
* @see https://www.drupal.org/node/1708692
*/
protected function mockStandardInstall() {
$this->drupalCreateContentType([
'type' => 'article',
]);
// Create the vocabulary for the tag field.
$this->vocabulary = Vocabulary::create([
'name' => 'Views testing tags',
'vid' => 'views_testing_tags',
]);
$this->vocabulary->save();
$field_name = 'field_' . $this->vocabulary->id();
Angie Byron
committed
$handler_settings = [
'target_bundles' => [
Angie Byron
committed
$this->vocabulary->id() => $this->vocabulary->id(),
],
Angie Byron
committed
'auto_create' => TRUE,
];
Angie Byron
committed
$this->createEntityReferenceField('node', 'article', $field_name, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED);
Alex Pott
committed
entity_get_form_display('node', 'article', 'default')
->setComponent($field_name, [
Angie Byron
committed
'type' => 'entity_reference_autocomplete_tags',
Alex Pott
committed
'weight' => -4,
])
Alex Pott
committed
->save();
entity_get_display('node', 'article', 'default')
->setComponent($field_name, [
Angie Byron
committed
'type' => 'entity_reference_label',
'weight' => 10,
])
->save();
entity_get_display('node', 'article', 'teaser')
->setComponent($field_name, [
Angie Byron
committed
'type' => 'entity_reference_label',
'weight' => 10,
])
->save();
}
/**
* Creates and returns a taxonomy term.
Angie Byron
committed
* @param array $settings
* (optional) An array of values to override the following default
* properties of the term:
* - name: A random string.
* - description: A random string.
* - format: First available text format.
* - vid: Vocabulary ID of self::$vocabulary object.
* - langcode: LANGCODE_NOT_SPECIFIED.
* Defaults to an empty array.
Angie Byron
committed
*
* @return \Drupal\taxonomy\Entity\Term
* The created taxonomy term.
*/
Angie Byron
committed
protected function createTerm(array $settings = []) {
Angie Byron
committed
$filter_formats = filter_formats();
$format = array_pop($filter_formats);
Angie Byron
committed
$settings += [
Alex Pott
committed
'name' => $this->randomMachineName(),
'description' => $this->randomMachineName(),
// Use the first available text format.
'format' => $format->id(),
'vid' => $this->vocabulary->id(),
'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
Angie Byron
committed
];
$term = Term::create($settings);
$term->save();
return $term;
}
}