Commit 19320114 authored by Saurabh Vijayvargiya's avatar Saurabh Vijayvargiya Committed by dmitriy.trt
Browse files

Issue #3309475 by sorabh.v6: Write tests to invalidate cache when settings are changed

parent 768ab1d7
Loading
Loading
Loading
Loading
+77 −0
Original line number Diff line number Diff line
@@ -154,4 +154,81 @@ class TraillessMenuTest extends BrowserTestBase {

    $this->assertSame($render_counter, $state->get('render_counter'));
  }

  /**
   * Tests that cache gets invalidated upon trailless_menu form save if form
   * setting is changed.
   */
  function testCacheInvalidationOnFormSave() {
    /** @var \Drupal\Core\Cache\DatabaseCacheTagsChecksum $cacheTagChecksum **/
    $cacheTagChecksum = \Drupal::service('cache_tags.invalidator.checksum');

    // Create menu links for footer menu.
    $this->first_item = MenuLinkContent::create([
      'link' => 'https://example.com',
      'title' => 'footer1',
      'menu_name' => 'footer',
    ]);
    $this->first_item->save();

    $this->second_item = MenuLinkContent::create([
      'link' => 'https://example.com/footer2',
      'title' => 'footer2',
      'menu_name' => 'footer',
    ]);
    $this->second_item->save();

    // Place footer menu on the page.
    $this->block = $this->drupalPlaceBlock(
      'system_menu_block:footer',
      [
        'label' => 'trailless_menu footer navigation'
      ]
    );

    // Goto home page or some other page.
    $this->drupalGet('');

    // Goto trailless menu config page.
    $this->drupalGet('/admin/config/user-interface/trailless-menu');

    // Check cachetag count for tag reset of main and footer.
    $mainMenuCount = $cacheTagChecksum
      ->getCurrentChecksum(['config:system.menu.main']);
    $footerMenuCount = $cacheTagChecksum
      ->getCurrentChecksum(['config:system.menu.footer']);

    // Tick footer in the config page and save config.
    $edit = [
      'trailless_menus[main]' => TRUE,
      'trailless_menus[footer]' => TRUE,
    ];
    $this->submitForm($edit, 'Save configuration');

    // Check that main menu cachetag count is same and for footer is changed.
    $newMainMenuCount = $cacheTagChecksum
      ->getCurrentChecksum(['config:system.menu.main']);
    $newFooterMenuCount = $cacheTagChecksum
      ->getCurrentChecksum(['config:system.menu.footer']);

    $this->assertSame($mainMenuCount, $newMainMenuCount);
    $this->assertNotSame($footerMenuCount, $newFooterMenuCount);

    // Untick main menu and save config.
    $this->drupalGet('/admin/config/user-interface/trailless-menu');
    $edit['trailless_menus[main]'] = FALSE;
    $this->submitForm($edit, 'Save configuration');

    // Check that footer menu cachetag count is same and for main menu is changed.
    $mainMenuCount = $newMainMenuCount;
    $footerMenuCount = $newFooterMenuCount;

    $newMainMenuCount = $cacheTagChecksum
      ->getCurrentChecksum(['config:system.menu.main']);
    $newFooterMenuCount = $cacheTagChecksum
      ->getCurrentChecksum(['config:system.menu.footer']);

    $this->assertNotSame($mainMenuCount, $newMainMenuCount);
    $this->assertSame($footerMenuCount, $newFooterMenuCount);
  }
}