LoadMultipleTest.php in Drupal 10
File
core/modules/taxonomy/tests/src/Functional/LoadMultipleTest.php
View source
<?php
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\taxonomy\Entity\Term;
class LoadMultipleTest extends TaxonomyTestBase {
protected $defaultTheme = 'stark';
protected function setUp() : void {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'administer taxonomy',
]));
}
public function testTaxonomyTermMultipleLoad() {
$vocabulary = $this
->createVocabulary();
$i = 0;
while ($i < 5) {
$i++;
$this
->createTerm($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,
]));
$terms2 = Term::loadMultiple(array_keys($terms));
$this
->assertEquals($terms, $terms2, 'Both arrays contain the same terms.');
$deleted = array_shift($terms2);
$deleted
->delete();
$deleted_term = Term::load($deleted
->id());
$this
->assertNull($deleted_term);
$terms3 = $term_storage
->loadByProperties([
'vid' => $vocabulary
->id(),
]);
$this
->assertCount(4, $terms3, 'Correct number of terms were loaded.');
$this
->assertFalse(isset($terms3[$deleted
->id()]));
$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.');
}
}