View source
<?php
namespace Drupal\simple_megamenu\Tests;
use Drupal\Core\Url;
use Drupal\simple_megamenu\Entity\SimpleMegaMenu;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\Tests\BrowserTestBase;
class SimpleMegaMenuTest extends BrowserTestBase {
public static $modules = [
'system',
'language',
'user',
'link',
'file',
'image',
'content_translation',
'text',
'menu_ui',
'menu_link_content',
'field',
'field_ui',
'simple_megamenu',
'simple_megamenu_example',
];
protected $defaultTheme = 'stark';
protected $user;
protected $normalUser;
protected $profile = 'standard';
protected function setUp() {
parent::setUp();
$this->normalUser = $this
->drupalCreateUser([
'view published simple mega menu entities',
]);
$this->user = $this
->drupalCreateUser([
'administer site configuration',
'administer menu',
'administer blocks',
'access simple mega menu entities canonical page',
'view published simple mega menu entities',
'view unpublished simple mega menu entities',
'add simple mega menu entities',
'edit simple mega menu entities',
'access simple mega menu overview',
]);
$this
->drupalLogin($this->user);
$block_main_menu_path = '/admin/structure/block/manage/stark_main_menu';
$edit = [
'settings[depth]' => '0',
];
$this
->drupalPostForm($block_main_menu_path, $edit, 'Save block');
}
public function createMenuLink($text, $path, $weight = 0, $menu = 'main', $expanded = 1, $parent = NULL, $langcode = 'en') {
$menu_link = MenuLinkContent::create([
'title' => $text,
'link' => [
'uri' => $path,
],
'menu_name' => $menu,
'weight' => $weight,
'expanded' => $expanded,
'langcode' => $langcode,
]);
if ($parent !== NULL) {
$menu_link
->set('parent', 'menu_link_content:' . $parent);
}
$menu_link
->save();
return $menu_link;
}
public function createMegaMenu($type, $title, $langcode, $uid = '1', array $fields = [], $status = TRUE) {
$mega_menu = SimpleMegaMenu::create([
'type' => $type,
'name' => $title,
'uid' => $uid,
'status' => $status,
'langcode' => $langcode,
]);
if (empty($fields)) {
$mega_menu
->save();
}
else {
foreach ($fields as $field_name => $value) {
if ($mega_menu
->hasField($field_name)) {
$mega_menu
->set($field_name, $value);
}
}
$mega_menu
->save();
}
return $mega_menu;
}
public function testMegaMenu() {
$content_path = '/admin/content/simple_mega_menu/add/megamenu';
$this
->drupalGet($content_path);
$this
->assertFieldByName('name[0][value]');
$this
->assertFieldByName('field_image_title[0][value]');
$this
->assertFieldByName('field_image_link[0][uri]');
$this
->assertFieldByName('field_text[0][value]');
$this
->assertFieldByName('field_links[0][uri]');
$this
->assertFieldByName('field_links[0][title]');
$this
->assertText('The name of the Simple mega menu entity.');
$this
->assertResponse(200);
$fields = [
'field_text' => [
0 => [
'value' => 'CTA text for mega menu',
],
],
'field_links' => [
0 => [
'uri' => 'https://www.flocondetoile.fr',
'title' => 'See Flocon de toile',
],
1 => [
'uri' => 'route:<front>',
'title' => 'Contact us',
],
],
];
$mega_menu = $this
->createMegaMenu('megamenu', 'Services', 'en', $this->user
->id(), $fields);
$menu_service = $this
->createMenuLink('Services main', 'route:<front>');
$this
->createMenuLink('Services child', 'route:<front>', 0, 'main', 1, $menu_service
->uuid());
$menu_path = '/admin/structure/menu/item/';
$this
->drupalGet($menu_path . $menu_service
->id() . '/edit');
$this
->assertResponse(200);
$this
->assertFieldByName('simple_mega_menu');
$edit = [
'simple_mega_menu' => $mega_menu
->label() . ' (' . $mega_menu
->id() . ')',
];
$this
->drupalPostForm($menu_path . $menu_service
->id() . '/edit', $edit, 'Save');
$this
->assertText('The menu link has been saved.');
$this
->drupalGet(Url::fromRoute('<front>'));
$this
->assertText('Services main');
$this
->assertText('CTA text for mega menu');
$this
->assertText('See Flocon de toile');
$this
->assertText('Contact us');
$this
->drupalGet('/admin/content/simple_mega_menu/' . $mega_menu
->id());
$this
->assertResponse('200');
$this
->drupalLogout();
$this
->drupalLogin($this->normalUser);
$this
->drupalGet('/admin/content/simple_mega_menu/' . $mega_menu
->id());
$this
->assertResponse('404');
$mega_menu2 = $this
->createMegaMenu('megamenu', 'Services2', 'en', $this->user
->id(), $fields);
$mega_menu2
->delete();
}
}