class LoadMultipleTest
Same name and namespace in other branches
- 11.x core/modules/taxonomy/tests/src/Kernel/LoadMultipleTest.php \Drupal\Tests\taxonomy\Kernel\LoadMultipleTest
Tests the loading of multiple taxonomy terms at once.
@group taxonomy
Hierarchy
- class \Drupal\Tests\BrowserTestBase uses \Drupal\Core\Test\FunctionalTestSetupTrait, \Drupal\Tests\UiHelperTrait, \Drupal\Core\Test\TestSetupTrait, \Drupal\Tests\block\Traits\BlockCreationTrait, \Drupal\FunctionalTests\AssertLegacyTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\node\Traits\NodeCreationTrait, \Drupal\Tests\node\Traits\ContentTypeCreationTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\user\Traits\UserCreationTrait, \Drupal\Tests\XdebugRequestTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait, \Drupal\Tests\ExtensionListTestTrait implements \PHPUnit\Framework\TestCase
- class \Drupal\Tests\taxonomy\Functional\TaxonomyTestBase uses \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait, \Drupal\Tests\field\Traits\EntityReferenceTestTrait implements \Drupal\Tests\BrowserTestBase
- class \Drupal\Tests\taxonomy\Functional\LoadMultipleTest implements \Drupal\Tests\taxonomy\Functional\TaxonomyTestBase
- class \Drupal\Tests\taxonomy\Functional\TaxonomyTestBase uses \Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait, \Drupal\Tests\field\Traits\EntityReferenceTestTrait implements \Drupal\Tests\BrowserTestBase
Expanded class hierarchy of LoadMultipleTest
File
-
core/
modules/ taxonomy/ tests/ src/ Functional/ LoadMultipleTest.php, line 13
Namespace
Drupal\Tests\taxonomy\FunctionalView source
class LoadMultipleTest extends TaxonomyTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
$this->drupalLogin($this->drupalCreateUser([
'administer taxonomy',
]));
}
/**
* Tests entity_load_multiple().
*/
public function testTaxonomyTermMultipleLoad() {
// Create a vocabulary.
$vocabulary = $this->createVocabulary();
// Create five terms in the vocabulary.
$i = 0;
while ($i < 5) {
$i++;
$this->createTerm($vocabulary);
}
// Load the terms from the vocabulary.
$term_storage = \Drupal::entityTypeManager()->getStorage('taxonomy_term');
$terms = $term_storage->loadByProperties([
'vid' => $vocabulary->id(),
]);
$count = count($terms);
$this->assertEquals(5, $count, new FormattableMarkup('Correct number of terms were loaded. @count terms.', [
'@count' => $count,
]));
// Load the same terms again by tid.
$terms2 = Term::loadMultiple(array_keys($terms));
$this->assertEquals($terms, $terms2, 'Both arrays contain the same terms.');
// Remove one term from the array, then delete it.
$deleted = array_shift($terms2);
$deleted->delete();
$deleted_term = Term::load($deleted->id());
$this->assertNull($deleted_term);
// Load terms from the vocabulary by vid.
$terms3 = $term_storage->loadByProperties([
'vid' => $vocabulary->id(),
]);
$this->assertCount(4, $terms3, 'Correct number of terms were loaded.');
$this->assertFalse(isset($terms3[$deleted->id()]));
// Create a single term and load it by name.
$term = $this->createTerm($vocabulary);
$loaded_terms = $term_storage->loadByProperties([
'name' => $term->getName(),
]);
$this->assertCount(1, $loaded_terms, 'One term was loaded.');
$loaded_term = reset($loaded_terms);
$this->assertEquals($term->id(), $loaded_term->id(), 'Term loaded by name successfully.');
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.