View source
<?php
namespace Drupal\Tests\poll\Functional;
class PollDeleteChoiceTest extends PollTestBase {
function testChoiceRemoval() {
$ids = \Drupal::entityQuery('poll_choice')
->condition('choice', $this->poll->choice[0]->entity
->label())
->execute();
$this
->assertEqual(count($ids), 1, 'Choice 1 exists in the database');
$edit = array(
'choice' => $this->poll->choice[1]->target_id,
);
$this
->drupalPostForm('poll/' . $this->poll
->id(), $edit, t('Vote'));
$xml = $this
->xpath('//dt[text()=:choice]/following-sibling::dd[1]/div', [
':choice' => $this->poll->choice[1]->entity
->label(),
]);
$this
->assertEqual(1, $xml[0]
->getAttribute('data-value'));
$this
->drupalGet("poll/" . $this->poll
->id() . "/edit");
$edit = [
'choice[0][choice]' => '',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->drupalGet('admin/content/poll');
$this
->clickLink($this->poll
->label());
$this
->assertNoText($this->poll->choice[0]->entity
->label());
$this
->assertText($this->poll->choice[1]->entity
->label());
$this
->assertText($this->poll->choice[2]->entity
->label());
$ids = \Drupal::entityQuery('poll_choice')
->condition('choice', $this->poll->choice[0]->entity
->label())
->execute();
$this
->assertEqual(count($ids), 0, 'Choice 1 has been deleted in the database');
$this
->drupalGet('poll/' . $this->poll
->id());
$vote = $this->poll->choice[1]->target_id;
$vote_recorded = \Drupal::database()
->query('SELECT chid FROM {poll_vote} WHERE chid = :chid', array(
':chid' => $vote,
))
->fetch();
$this
->assertFalse(empty($vote_recorded), 'Vote in Choice 2 still in the database');
$xml = $this
->xpath('//dt[text()=:choice]/following-sibling::dd[1]/div', [
':choice' => $this->poll->choice[1]->entity
->label(),
]);
$this
->assertEqual(1, $xml[0]
->getAttribute('data-value'));
$this
->drupalGet("poll/" . $this->poll
->id() . "/edit");
$edit = [
'choice[0][choice]' => '',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->drupalGet('admin/content/poll');
$this
->clickLink($this->poll
->label());
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertTrue(isset($elements[0]), "vote deleted successfully");
$vote_deleted = \Drupal::database()
->query('SELECT chid FROM {poll_vote} WHERE chid = :chid', array(
':chid' => $vote,
))
->fetch();
$this
->assertTrue(empty($vote_deleted), 'Vote in Choice 2 has been deleted from the database');
}
}