Commit d8d887f8 authored by George's avatar George
Browse files

Issue #3192574 by szhu: Add automated tests

parent afaa8d8b
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
# Global configuration settings.
anti_duplicates.settings:
  type: config_object
  label: 'Anti Duplicates global settings'
  mapping:
    anti_duplicates_message:
      type: text_format
      label: 'Message'
    anti_duplicates_form_submission:
      type: boolean
      label: 'Disable form submission'
    anti_duplicates_search_type:
      type: integer
      label: 'Search type'
+38 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\anti_duplicates\Functional;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;

/**
 * Test the Anti duplicates install without errors.
 *
 * For example, if parts of the anti_duplicates config schema are
 * missing, then the module will not install correctly. Note that Unit and
 * Kernel tests don't fully install the module.
 *
 * @group anti_duplicates
 */
class InstallTest extends WebDriverTestBase {

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['anti_duplicates'];

  /**
   * Assert that the anti_duplicates module installed correctly.
   */
  public function testModuleInstalls() {
    // If we get here, then the module was successfully installed during the
    // setUp phase without throwing any Exceptions. Assert that TRUE is true,
    // so at least one assertion runs, and then exit.
    $this->assertTrue(TRUE, 'Module installed correctly.');
  }

}
+102 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\anti_duplicates\FunctionalJavascript;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\anti_duplicates\Traits\AntiDuplicatesFunctionalTestTrait;
use Drupal\Tests\RandomGeneratorTrait;

/**
 * Test that duplicate nodes can be detected.
 *
 * @group anti_duplicates
 */
class DetectDuplicateNodeTest extends WebDriverTestBase {
  use AntiDuplicatesFunctionalTestTrait;
  use RandomGeneratorTrait;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['anti_duplicates', 'node'];

  /**
   * {@inheritdoc}
   */
  protected $sutNodeTitle;

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    // Change anti-duplicates global settings to a known-good state for this
    // test (i.e.: so changing defaults doesn't make this test break).
    $this->antiDuplicatesChangeFoundMessage('Duplicate found');
    $this->antiDuplicatesEnableFormSubmission();
    $this->antiDuplicatesSearchType(0);

    $this->setUpArticleAndPageContentTypes();

    // Create node with a random title and body.
    $this->sutNodeTitle = $this->randomString();
    $this->drupalCreateNode([
      'type' => 'page',
      'title' => $this->sutNodeTitle,
      'body' => '',
    ]);

    $this->setUpAdminUser();
  }

  /**
   * Test the anti duplicates is working with twp different content types.
   */
  public function testCreateAnotherDifferentContentType() {
    $this->drupalLogin($this->sutAntiDuplicateAdminUser);
    $this->drupalGet('node/add/article');
    $page = $this->getSession()->getPage();

    // Check there is no duplicated title value.
    $adSimilarContentListBefore = $page->find('css', 'ul#dw-listing');
    $this->assertNull($adSimilarContentListBefore);

    // Enter the same title on the current page.
    $titleField = $page->find('css', 'input[data-drupal-selector="edit-title-0-value"]');
    $titleField->setValue($this->sutNodeTitle);
    $this->getSession()->wait(5000, 'document.querySelector("ul#dw-listing")');

    // Check that duplicated title value visible after title entered.
    $adSimilarContentListAfter = $page->find('css', 'ul#dw-listing');
    $this->assertNotNull($adSimilarContentListAfter);
  }

  /**
   * Test the anti duplicates is working with same content type.
   */
  public function testCreateAnotherSameContentType() {
    $this->drupalLogin($this->sutAntiDuplicateAdminUser);
    $this->drupalGet('node/add/page');
    $page = $this->getSession()->getPage();

    // Check there is no duplicated title value.
    $adSimilarContentListBefore = $page->find('css', 'ul#dw-listing');
    $this->assertNull($adSimilarContentListBefore);

    // Enter the same title on the current page.
    $titleField = $page->find('css', 'input[data-drupal-selector="edit-title-0-value"]');
    $titleField->setValue($this->sutNodeTitle);
    $this->getSession()->wait(5000, 'document.querySelector("ul#dw-listing")');

    // Check that duplicated title value visible after title entered.
    $adSimilarContentListAfter = $page->find('css', 'ul#dw-listing');
    $this->assertNotNull($adSimilarContentListAfter);
  }

}
+185 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\anti_duplicates\FunctionalJavascript;

use Drupal\FunctionalJavascriptTests\WebDriverTestBase;
use Drupal\Tests\anti_duplicates\Traits\AntiDuplicatesFunctionalTestTrait;

/**
 * Test Anti Duplicates global configuration form.
 *
 * @group anti_duplicates
 */
class GlobalConfigurationTest extends WebDriverTestBase {
  use AntiDuplicatesFunctionalTestTrait;

  /**
   * {@inheritdoc}
   */
  protected $defaultTheme = 'stark';

  /**
   * {@inheritdoc}
   */
  protected static $modules = ['anti_duplicates', 'node'];

  /**
   * {@inheritdoc}
   */
  public function setUp() {
    parent::setUp();

    $this->setUpArticleAndPageContentTypes();
    $this->setUpAdminUser();
    $this->drupalLogin($this->sutAntiDuplicateAdminUser);
  }

  /**
   * Test that we can stop the form from being submitted when duplicates exist.
   */
  public function testDisableFormSubmitWhenDuplicatesExistConfig() {
    // Change other anti-duplicates global settings to a known-good state for
    // this test (i.e.: so changing defaults for those other settings doesn't
    // break this test method).
    $this->antiDuplicatesChangeFoundMessage('Duplicate found');
    $this->antiDuplicatesSearchType(0);

    // Create a node with a random string.
    $sutNodeTitle = $this->randomString();
    $this->drupalCreateNode([
      'type' => 'page',
      'title' => $sutNodeTitle,
      'body' => '',
    ]);

    // Disable form submission.
    $this->antiDuplicatesDisableFormSubmission();

    // Load a page where anti-duplicates runs.
    $this->drupalGet('node/add/page');
    $page = $this->getSession()->getPage();

    // Check there is initially no duplicated title value.
    $adSimilarContentListBefore = $page->find('css', 'ul#dw-listing');
    $this->assertNull($adSimilarContentListBefore);

    // Populate the title field with the duplicate title.
    $titleField = $page->find('css', 'input[data-drupal-selector="edit-title-0-value"]');
    $titleField->setValue($sutNodeTitle);
    $this->getSession()->wait(5000, 'document.querySelector("ul#dw-listing")');

    // Check that duplicated title value visible after title entered.
    $adSimilarContentListAfter = $page->find('css', 'ul#dw-listing');
    $this->assertNotNull($adSimilarContentListAfter);

    // Verify that the submit button is disabled, as per the "disable form
    // submission" global preference.
    $button = $page->find('css', '#edit-submit');
    $button_disabled = $button->hasAttribute('disabled');

    // Click the "Not a duplicate" button to enable submit button, then verify
    // that it is enabled.
    $not_a_dulpicate = '#dw-enable-form';
    $this->click($not_a_dulpicate);
    $node_submit_button = $page->find('css', '#edit-submit');
    $this->assertTrue($button_disabled);
  }

  /**
   * Test the different options with search type.
   */
  public function testSearchType() {
    // Change other anti-duplicates global settings to a known-good state for
    // this test (i.e.: so changing defaults for those other settings doesn't
    // break this test method).
    $this->antiDuplicatesChangeFoundMessage('Duplicate found');
    $this->antiDuplicatesEnableFormSubmission();

    // Create a variable number of random words for a title.
    $sutNodeTitle = 'ONE TWO THREE';

    // Create initial node.
    $this->drupalCreateNode([
      'type' => 'page',
      'title' => $sutNodeTitle,
      'body' => '',
    ]);

    //
    // Test search type 0: 'Contains a sequence of the title keywords': must
    // contain all words from the original in the same order, but can have words
    // between.
    //
    $this->antiDuplicatesSearchType(0);
    $this->drupalGet('node/add/page');
    $page = $this->getSession()->getPage();

    $newTitle = 'lorem ONE ipsum TWO dolor THREE sit';

    $titleField = $page->find('css', 'input[data-drupal-selector="edit-title-0-value"]');
    $titleField->setValue($newTitle);
    $this->getSession()->wait(5000, 'document.querySelector("ul#dw-listing")');
    $adSimilarContentListAfter = $page->find('css', 'ul#dw-listing');
    $this->assertNotNull($adSimilarContentListAfter);

    //
    // Test search type 1: 'Contains the exact title': must contain the exact
    // title, but can have other words before and after.
    //
    $this->antiDuplicatesSearchType(1);
    $this->drupalGet('node/add/page');
    $page = $this->getSession()->getPage();

    $newTitle = 'lorem ONE TWO THREE ipsum';

    $titleField = $page->find('css', 'input[data-drupal-selector="edit-title-0-value"]');
    $titleField->setValue($newTitle);
    $this->getSession()->wait(5000, 'document.querySelector("ul#dw-listing")');
    $adSimilarContentListAfter = $page->find('css', 'ul#dw-listing');
    $this->assertNotNull($adSimilarContentListAfter);

    //
    // Test search type 2: 'Contains any word from the title': at least one word
    // from the title is present.
    //
    $this->antiDuplicatesSearchType(2);
    $this->drupalGet('node/add/page');
    $page = $this->getSession()->getPage();

    $newTitle = 'lorem ipsum dolor TWO sit';

    $titleField = $page->find('css', 'input[data-drupal-selector="edit-title-0-value"]');
    $titleField->setValue($newTitle);
    $this->getSession()->wait(5000, 'document.querySelector("ul#dw-listing")');
    $adSimilarContentListAfter = $page->find('css', 'ul#dw-listing');
    $this->assertNotNull($adSimilarContentListAfter);
  }

  /**
   * Test that it is possible to configure the "duplicates found" message.
   */
  public function testCanChangeDuplicatesFoundMessage() {
    // Change other anti-duplicates global settings to a known-good state for
    // this test (i.e.: so changing defaults for those other settings doesn't
    // break this test method).
    $this->antiDuplicatesEnableFormSubmission();
    $this->antiDuplicatesSearchType(0);

    $sutNodeTitle = $this->randomString();
    $this->drupalCreateNode([
      'type' => 'page',
      'title' => $sutNodeTitle,
      'body' => '',
    ]);

    $newMessage = 'Duplicated content list below';
    $this->antiDuplicatesChangeFoundMessage($newMessage);

    // Load a page where the message will be shown.
    $this->drupalGet('node/add/page');
    $assertSession = $this->assertSession();

    $assertSession->pageTextContains($newMessage);
  }

}
+87 −0
Original line number Diff line number Diff line
<?php

namespace Drupal\Tests\anti_duplicates\Traits;

/**
 * Simplify working with functional test settings.
 */
trait AntiDuplicatesFunctionalTestTrait {

  /**
   * A user with permission to add content and administer anti duplicate.
   *
   * @var \Drupal\user\UserInterface
   */
  protected $sutAntiDuplicateAdminUser;

  /**
   * Creates an article node type, and a page node type.
   */
  public function setUpArticleAndPageContentTypes() {
    // Create Article content type.
    $type = $this->container->get('entity_type.manager')->getStorage('node_type')
      ->create([
        'type' => 'article',
        'name' => 'Article',
      ]);
    $type->save();

    // Create Basic Page content type.
    $type = $this->container->get('entity_type.manager')->getStorage('node_type')
      ->create([
        'type' => 'page',
        'name' => 'Basic Page',
      ]);
    $type->save();
  }

  /**
   * Create user with perms to create nodes and change anti_duplicates settings.
   */
  public function setUpAdminUser() {
    $this->sutAntiDuplicateAdminUser = $this->drupalCreateUser([
      'access content',
      'create page content',
      'create article content',
      'administer anti_duplicates',
      'administer nodes',
    ]);
  }

  /**
   * Change default displayed message in Related Content section on node page.
   */
  public function antiDuplicatesChangeFoundMessage($message) {
    $config = \Drupal::configFactory()->getEditable('anti_duplicates.settings');
    $config->set('anti_duplicates_message.value', $message);
    $config->save();
  }

  /**
   * Disable form submission if there is duplication.
   */
  public function antiDuplicatesDisableFormSubmission() {
    $config = \Drupal::configFactory()->getEditable('anti_duplicates.settings');
    $config->set('anti_duplicates_form_submission', TRUE);
    $config->save();
  }

  /**
   * Enable form submission if there is duplication.
   */
  public function antiDuplicatesEnableFormSubmission() {
    $config = \Drupal::configFactory()->getEditable('anti_duplicates.settings');
    $config->set('anti_duplicates_form_submission', FALSE);
    $config->save();
  }

  /**
   * Change the anti-duplicates detection method.
   */
  public function antiDuplicatesSearchType($newType) {
    $config = \Drupal::configFactory()->getEditable('anti_duplicates.settings');
    $config->set('anti_duplicates_search_type', $newType);
    $config->save();
  }

}