You are here

private function ForumAccessTestCase::testForumAccessDeleteTopic in Forum Access 6

This function test if the user can delete a topic Four steps: is there a edit link, does the edit page opens, is there a delete link on the edit page and can the topic be deleted

1 call to ForumAccessTestCase::testForumAccessDeleteTopic()
ForumAccessTestCase::testForumAccessRun in ./forum_access.test
The main function which is used to start testing a specific forum configuration.

File

./forum_access.test, line 725
Test file for forum_access.module.

Class

ForumAccessTestCase
This is the base class for forum access testing.

Code

private function testForumAccessDeleteTopic($topic_id = 0) {
  if (intval($topic_id) > 0) {
    $allowed_edit = $this
      ->testForumAccessAllowed('topic_update', $topic_id);
    $allowed_delete = $this
      ->testForumAccessAllowed('topic_delete', $topic_id);

    // Check to see if there is an edit link on the page
    $this
      ->drupalGet('node/' . $topic_id);
    if ($allowed_edit) {
      if (!$this
        ->assertPattern("/node\\/{$topic_id}\\/edit/", t('For @user there should be an edit-this-topic-link on the page: @page', array(
        '@user' => $this->loggedInUser ? t('this user') : t('anonymous'),
        '@page' => $this
          ->getUrl(),
      )))) {
        $this
          ->testForumAccessDumpDebugInfo('topic', $topic_id);
      }
    }
    else {
      if (!$this
        ->assertNoPattern("/node\\/{$topic_id}\\/edit/", t('For @user there should NOT be an edit-this-topic-link on the page: @page', array(
        '@user' => $this->loggedInUser ? t('this user') : t('anonymous'),
        '@page' => $this
          ->getUrl(),
      )))) {
        $this
          ->testForumAccessDumpDebugInfo('topic', $topic_id);
      }
    }

    // Check to see if the user/anonymous is allowed to open the edit page
    $page_opened = FALSE;
    $this
      ->drupalGet('node/' . $topic_id . '/edit/');
    if ($allowed_edit) {
      if (!$this
        ->assertNoText("not authorized", t('@user should be allowed to open the edit page on this topic: @page', array(
        '@user' => $this->loggedInUser ? t('The user') : t('Anonymous'),
        '@page' => $this
          ->getUrl(),
      )))) {
        $this
          ->testForumAccessDumpDebugInfo('topic', $topic_id);
      }
      else {
        $page_opened = TRUE;
      }
    }
    else {
      if (!$this
        ->assertText("not authorized", t('@user should NOT be allowed to open the edit page on this topic: @page', array(
        '@user' => $this->loggedInUser ? t('The user') : t('Anonymous'),
        '@page' => $this
          ->getUrl(),
      )))) {
        $this
          ->testForumAccessDumpDebugInfo('topic', $topic_id);
        $page_opened = TRUE;
      }
    }
    $page_has_delete_button = $test_direct_link = FALSE;
    if ($page_opened) {

      // Check to see if there is an delete link on the edit page
      $this
        ->drupalGet('node/' . $topic_id . '/edit');
      if ($allowed_delete) {
        if (!$this
          ->assertFieldById('edit-delete', 'Delete', t('For @user there should be a delete-link on the edit page for this topic: @page', array(
          '@user' => $this->loggedInUser ? t('this user') : t('anonymous'),
          '@page' => $this
            ->getUrl(),
        )))) {
          $this
            ->testForumAccessDumpDebugInfo('topic', $topic_id);
        }
        else {
          $page_has_delete_button = TRUE;
        }
      }
      else {
        if (!$this
          ->assertNoFieldById('edit-delete', 'Delete', t('For @user there should NOT be a delete-link on the edit page for this topic: @page', array(
          '@user' => $this->loggedInUser ? t('this user') : t('anonymous'),
          '@page' => $this
            ->getUrl(),
        )))) {
          $this
            ->testForumAccessDumpDebugInfo('topic', $topic_id);
          $page_has_delete_button = TRUE;
        }
      }
      if ($page_has_delete_button) {

        // Check to see if the user/anonymous is allowed to delete the topic
        $this
          ->drupalPost('node/' . $topic_id . '/edit', array(), t('Delete'));
      }
    }
    else {
      $this
        ->drupalGet('node/' . $topic_id . '/delete');
      $test_direct_link = TRUE;
    }
    if ($page_has_delete_button || $test_direct_link) {
      if ($allowed_delete) {
        if (!$this
          ->assertNoText("not authorized", t('@user should be allowed to delete this topic: @page', array(
          '@user' => $this->loggedInUser ? t('The user') : t('Anonymous'),
          '@page' => $this
            ->getUrl(),
        )))) {
          $this
            ->testForumAccessDumpDebugInfo('topic', $topic_id);
        }
      }
      else {
        if (!$this
          ->assertText("not authorized", t('@user should NOT be allowed to delete this topic: @page', array(
          '@user' => $this->loggedInUser ? t('The user') : t('Anonymous'),
          '@page' => $this
            ->getUrl(),
        )))) {
          $this
            ->testForumAccessDumpDebugInfo('topic', $topic_id);
        }
      }
    }
  }
}