PollCreateTest.php in Poll 8
File
tests/src/Functional/PollCreateTest.php
View source
<?php
namespace Drupal\Tests\poll\Functional;
class PollCreateTest extends PollTestBase {
public function testPollCreate() {
$poll = $this->poll;
$this
->drupalLogin($this->admin_user);
$this
->drupalGet('poll/' . $poll
->id() . '/edit');
$this
->assertText($poll
->label());
$this
->drupalGet('admin/content/poll', [
'query' => [
'status' => '2',
],
]);
$this
->assertNoText($poll
->label());
$this
->drupalGet('admin/content/poll');
$this
->assertText($poll
->label());
$this
->assertText('Y');
$this
->clickLink($poll
->label());
$new_question = $this
->randomMachineName();
$poll
->setQuestion($new_question);
$poll
->save();
$this
->drupalGet('poll/' . $poll
->id() . '/edit');
$this
->assertText($new_question);
$vote_choice = $this
->randomMachineName();
$poll->choice[0]->entity
->setChoice($vote_choice);
$poll->choice[0]->entity
->save();
$this
->drupalGet('poll/' . $poll
->id() . '/edit');
$this
->assertFieldByXPath("//input[@name='choice[0][choice]']", $vote_choice, 'Choice successfully changed.');
}
function testPollClose() {
$poll = $this->poll;
$poll
->close();
$poll
->save();
$this
->drupalLogin($this->web_user);
$this
->drupalGet('poll/add');
$this
->assertResponse(403);
$this
->drupalGet('poll/' . $poll
->id());
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertTrue(empty($elements), "Vote button doesn't appear.");
$elements = $this
->xpath('value="View poll"');
$this
->assertTrue(empty($elements), "View poll button doesn't appear.");
$poll
->open();
$poll
->save();
$this
->drupalGet('poll/' . $poll
->id());
$elements = $this
->xpath('//input[@value="Vote"]');
$this
->assertFalse(empty($elements), "Vote button appears.");
$edit = array(
'choice' => 1,
);
$this
->drupalPostForm(NULL, $edit, t('Vote'));
$this
->assertText('Your vote has been recorded.');
$elements = $this
->xpath('//input[@value="Cancel vote"]');
$this
->assertTrue(isset($elements[0]), "'Cancel vote' button appears.");
$poll
->close();
$poll
->save();
$this
->drupalGet('poll/' . $poll
->id());
$elements = $this
->xpath('//input[@value="Cancel your vote"]');
$this
->assertTrue(empty($elements), "'Cancel your vote' button no longer appears.");
}
function testwithRestrictedUser() {
$admin_poll = $this->poll;
$account = $this
->drupalCreateUser([
'create polls',
]);
$this
->drupalLogin($account);
$this
->drupalGet('poll/add');
$this
->assertResponse(200);
$this
->assertNoFieldByName('uid[0][target_id]', NULL);
$own_poll = $this
->pollCreate(7, $account);
$this
->drupalGet('poll/' . $admin_poll
->id() . '/edit');
$this
->assertResponse(403);
$this
->drupalGet('poll/' . $own_poll
->id() . '/edit');
$this
->assertResponse(403);
$account = $this
->drupalCreateUser([
'create polls',
'edit own polls',
]);
$this
->drupalLogin($account);
$own_poll = $this
->pollCreate(7, $account);
$this
->drupalGet('poll/' . $admin_poll
->id() . '/edit');
$this
->assertResponse(403);
$this
->drupalGet('poll/' . $own_poll
->id() . '/edit');
$this
->assertResponse(200);
$this
->assertNoFieldByName('uid[0][target_id]', NULL);
}
}