You are here

public function SimpleMegaMenuTest::testMegaMenu in Simple Mega Menu 2.0.x

Same name and namespace in other branches
  1. 8 src/Tests/SimpleMegaMenuTest.php \Drupal\simple_megamenu\Tests\SimpleMegaMenuTest::testMegaMenu()

Tests that the home page loads with a 200 response.

File

src/Tests/SimpleMegaMenuTest.php, line 184

Class

SimpleMegaMenuTest
Simple test to ensure that main page loads with module enabled.

Namespace

Drupal\simple_megamenu\Tests

Code

public function testMegaMenu() {
  $content_path = '/admin/content/simple_mega_menu/add/megamenu';

  // Check that the field appears in the form.
  $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);

  // Create a mage menu entity with some fields filled.
  $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',
      ],
    ],
  ];

  /* @var \Drupal\simple_megamenu\Entity\SimpleMegaMenu $mega_menu */
  $mega_menu = $this
    ->createMegaMenu('megamenu', 'Services', 'en', $this->user
    ->id(), $fields);

  // Create some Menu link content.
  $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');

  // Check the Mega menu is present.
  $this
    ->assertText('CTA text for mega menu');
  $this
    ->assertText('See Flocon de toile');
  $this
    ->assertText('Contact us');

  // Access to the canonical page.
  $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');

  /* @var \Drupal\simple_megamenu\Entity\SimpleMegaMenu $mega_menu */
  $mega_menu2 = $this
    ->createMegaMenu('megamenu', 'Services2', 'en', $this->user
    ->id(), $fields);
  $mega_menu2
    ->delete();
}