View source
<?php
namespace Drupal\Tests\devel_generate\Functional;
use Drupal\comment\Entity\Comment;
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;
class DevelGenerateCommandsTest extends BrowserTestBase {
use DrushTestTrait;
use DevelGenerateSetupTrait;
use MediaTypeCreationTrait;
protected static $modules = [
'comment',
'content_translation',
'devel',
'devel_generate',
'language',
'media',
'menu_ui',
'node',
'path',
'taxonomy',
];
protected $defaultTheme = 'stark';
public function setUp() {
parent::setUp();
$this
->setUpData();
}
public function testDrushGenerateUsers() {
$this
->drush('devel-generate-users', [
55,
], [
'kill' => NULL,
'roles' => 'administrator',
]);
$user = User::load(55);
$this
->assertTrue($user
->hasRole('administrator'));
}
public function testDrushGenerateTerms() {
$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($term
->language()
->getId(), 'fr');
$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() {
$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() {
$this
->drush('devel-generate-menus', [
1,
5,
], [
'kill' => NULL,
]);
$menus = Menu::loadMultiple();
foreach ($menus as $menu) {
if (strstr($menu
->id(), 'devel-') !== FALSE) {
break;
}
}
$link = MenuLinkContent::load(5);
$this
->assertEquals($menu
->id(), $link
->getMenuName());
}
public function testDrushGenerateContent() {
$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')
->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',
]);
$nodes = \Drupal::entityQuery('node')
->execute();
$node = Node::load(end($nodes));
$this
->assertEquals($node
->language()
->getId(), 'fr');
$this
->drush('devel-generate-content', [
18,
], [
'kill' => NULL,
'languages' => 'fr',
'translations' => 'de',
]);
$articles = \Drupal::entityQuery('node')
->condition('type', 'article')
->execute();
$pages = \Drupal::entityQuery('node')
->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 -v', [
9,
], [
'kill' => NULL,
'bundles' => 'page',
'add-type-label' => NULL,
]);
$nodes = \Drupal::entityQuery('node')
->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($node->title->value, 0, 13));
$this
->drush('devel-generate-content -v', [
10,
], [
'kill' => NULL,
'bundles' => 'article',
'authors' => '2',
]);
$nodes = \Drupal::entityQuery('node')
->condition('type', 'article')
->condition('uid', [
'2',
], 'IN')
->execute();
$this
->assertCount(10, $nodes);
}
public function testDrushGenerateMedia() {
$media_type1 = $this
->createMediaType('image');
$media_type2 = $this
->createMediaType('audio_file');
$this
->drush('devel-generate-media', [
53,
], [
'kill' => NULL,
]);
$this
->assertCount(53, \Drupal::entityQuery('media')
->execute());
$messages = $this
->getErrorOutput();
$this
->assertStringContainsStringIgnoringCase('Finished 53 elements created successfully.', $messages, 'devel-generate-media batch ending message not found');
$this
->drush('devel-generate-media', [
7,
], [
'media-types' => $media_type1
->id() . ',' . $media_type2
->id(),
'kill' => NULL,
]);
$this
->assertCount(7, \Drupal::entityQuery('media')
->execute());
}
}