DevelGenerateCommandsTest.php
View source
<?php
namespace Drupal\Tests\devel_generate\Functional;
use Drupal\comment\Entity\Comment;
use Drupal\media\Entity\Media;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\node\Entity\Node;
use Drupal\system\Entity\Menu;
use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\devel_generate\Traits\DevelGenerateSetupTrait;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\user\Entity\User;
use Drush\TestTraits\DrushTestTrait;
use PHPUnit\Framework\Attributes\Group;
class DevelGenerateCommandsTest extends BrowserTestBase {
use DrushTestTrait;
use DevelGenerateSetupTrait;
use MediaTypeCreationTrait;
protected static $modules = [
'comment',
'content_translation',
'devel',
'devel_generate',
'devel_generate_fields',
'language',
'media',
'menu_ui',
'node',
'path',
'taxonomy',
];
protected $defaultTheme = 'stark';
public function setUp() : void {
parent::setUp();
$this->setUpData();
}
public function testDrushGenerateUsers() : void {
$this->drush('devel-generate:users', [
'55',
], [
'kill' => NULL,
'roles' => 'administrator',
]);
$user = User::load(55);
$this->assertTrue($user->hasRole('administrator'));
}
public function testDrushGenerateTerms() : void {
$this->drush('devel-generate:terms', [
'55',
], [
'kill' => NULL,
'bundles' => $this->vocabulary
->id(),
]);
$term = Term::load(55);
$this->assertEquals($this->vocabulary
->id(), $term->bundle());
$this->drush('devel-generate:terms', [
'10',
], [
'kill' => NULL,
'bundles' => $this->vocabulary
->id(),
'languages' => 'fr',
]);
$term = Term::load(60);
$this->assertEquals('fr', $term->language()
->getId());
$this->drush('devel-generate:terms', [
'10',
], [
'kill' => NULL,
'bundles' => $this->vocabulary
->id(),
'languages' => 'fr',
'translations' => 'de',
]);
$term = Term::load(70);
$this->assertTrue($term->hasTranslation('de'));
$this->assertTrue($term->hasTranslation('fr'));
}
public function testDrushGenerateVocabs() : void {
$this->drush('devel-generate:vocabs', [
'5',
], [
'kill' => NULL,
]);
$vocabs = Vocabulary::loadMultiple();
$this->assertGreaterThan(4, count($vocabs));
$vocab = array_pop($vocabs);
$this->assertNotEmpty($vocab);
}
public function testDrushGenerateMenus() : void {
$generatedMenu = NULL;
$this->drush('devel-generate:menus', [
'1',
'5',
], [
'kill' => NULL,
]);
$menus = Menu::loadMultiple();
foreach ($menus as $menu) {
if (str_contains((string) $menu->id(), 'devel-')) {
$generatedMenu = $menu;
break;
}
}
$link = MenuLinkContent::load(5);
$this->assertNotNull($generatedMenu, 'Generated menu successfully.');
$this->assertNotNull($link, 'Generated link successfully.');
$this->assertEquals($generatedMenu->id(), $link->getMenuName(), 'Generated menu ID matches link menu name.');
}
public function testDrushGenerateContent() : void {
$this->drush('devel-generate:content', [
'21',
]);
$node = Node::load(21);
$this->assertNotEmpty($node);
$this->drush('devel-generate:content', [
'30',
'9',
], [
'kill' => NULL,
'bundles' => 'article',
]);
$comment = Comment::load(1);
$this->assertNotEmpty($comment);
$this->drush('devel-generate:content', [
'55',
], [
'kill' => NULL,
]);
$nodes = \Drupal::entityQuery('node')->accessCheck(FALSE)
->execute();
$this->assertCount(55, $nodes);
$messages = $this->getErrorOutput();
$this->assertStringContainsStringIgnoringCase('Finished 55 elements created successfully.', $messages, 'devel-generate-content batch ending message not found');
$this->drush('devel-generate:content', [
'10',
], [
'kill' => NULL,
'languages' => 'fr',
'base-fields' => 'phish',
]);
$nodes = \Drupal::entityQuery('node')->accessCheck(FALSE)
->execute();
$node = Node::load(end($nodes));
$this->assertEquals('fr', $node->language()
->getId());
$this->assertNotEmpty($node->get('phish')
->getString());
$this->drush('devel-generate:content', [
'18',
], [
'kill' => NULL,
'languages' => 'fr',
'translations' => 'de',
]);
$articles = \Drupal::entityQuery('node')->accessCheck(FALSE)
->condition('type', 'article')
->execute();
$pages = \Drupal::entityQuery('node')->accessCheck(FALSE)
->condition('type', 'page')
->execute();
$this->assertCount(18, $articles + $pages);
$node = Node::load(end($articles));
$this->assertTrue($node->hasTranslation('de'));
$this->assertTrue($node->hasTranslation('fr'));
$this->assertFalse($node->hasTranslation('ca'));
$this->drush('devel-generate:content', [
'9',
], [
'kill' => NULL,
'bundles' => 'page',
'add-type-label' => NULL,
]);
$nodes = \Drupal::entityQuery('node')->accessCheck(FALSE)
->condition('type', 'page')
->execute();
$this->assertCount(9, $nodes);
$messages = $this->getErrorOutput();
$this->assertStringContainsStringIgnoringCase('Created 9 nodes', $messages, 'batch end message not found');
$node = Node::load(end($nodes));
$this->assertEquals('Basic Page - ', substr((string) $node->title->value, 0, 13));
$this->drush('devel-generate:content', [
'10',
], [
'kill' => NULL,
'bundles' => 'article',
'authors' => '2',
]);
$nodes = \Drupal::entityQuery('node')->accessCheck(FALSE)
->condition('type', 'article')
->condition('uid', [
'2',
], 'IN')
->execute();
$this->assertCount(10, $nodes);
$userA = $this->drupalCreateUser([
'access content',
]);
$roleA = $userA->getRoles()[1];
$this->drush('devel-generate:content', [
'8',
], [
'kill' => NULL,
'bundles' => 'page',
'roles' => $roleA,
]);
$nodesA = \Drupal::entityQuery('node')->accessCheck(FALSE)
->condition('type', 'page')
->condition('uid', $userA->id())
->execute();
$this->assertCount(8, $nodesA, 'User A should have all the generated content');
$userB = $this->drupalCreateUser([
'create page content',
]);
$roleB = $userB->getRoles()[1];
$this->drush('devel-generate:content', [
'20',
], [
'kill' => NULL,
'bundles' => 'page',
'roles' => sprintf('%s, %s', $roleA, $roleB),
]);
$nodesA = \Drupal::entityQuery('node')->accessCheck(FALSE)
->condition('type', 'page')
->condition('uid', $userA->id())
->execute();
$nodesB = \Drupal::entityQuery('node')->accessCheck(FALSE)
->condition('type', 'page')
->condition('uid', $userB->id())
->execute();
$this->assertGreaterThan(0, count($nodesA), 'User A should have some content');
$this->assertGreaterThan(0, count($nodesB), 'User B should have some content');
$this->assertCount(20, $nodesA + $nodesB);
}
public function testDrushGenerateMedia() : void {
$media_type1 = $this->createMediaType('image');
$media_type2 = $this->createMediaType('audio_file');
$this->drush('devel-generate:media', [
'53',
], [
'kill' => NULL,
'base-fields' => 'phish',
]);
$this->assertCount(53, \Drupal::entityQuery('media')->accessCheck(FALSE)
->execute());
$messages = $this->getErrorOutput();
$this->assertStringContainsStringIgnoringCase('Finished 53 elements created successfully.', $messages, 'devel-generate-media batch ending message not found');
$medias = \Drupal::entityQuery('media')->accessCheck(FALSE)
->execute();
$media = Media::load(end($medias));
$this->assertNotEmpty($media->get('phish')
->getString());
$this->drush('devel-generate:media', [
'7',
], [
'media-types' => $media_type1->id() . ',' . $media_type2->id(),
'kill' => NULL,
]);
$this->assertCount(7, \Drupal::entityQuery('media')->accessCheck(FALSE)
->execute());
}
}