/**
  * @covers       tfrommen\ThatWasHelpful\Models\Nonce::is_valid
  * @dataProvider provide_is_valid_data
  *
  * @param bool   $response
  * @param string $nonce
  * @param string $action
  * @param int    $times
  * @param string $request_value
  */
 public function test_is_valid($response, $nonce, $action, $times, $request_value)
 {
     $class_action = 'class_action';
     $name = 'name';
     $testee = new Testee($class_action, $name);
     $_REQUEST[$name] = $request_value;
     WP_Mock::wpFunction('wp_verify_nonce', array('times' => $times, 'args' => array(WP_Mock\Functions::type('string'), WP_Mock\Functions::type('string')), 'return' => function ($nonce, $nonce_action) use($action, $class_action) {
         return $nonce === 'nonce' && ($nonce_action === 'action' || $nonce_action === $class_action && $action === '');
     }));
     $this->assertSame($response, $testee->is_valid($nonce, $action));
     $this->assertConditionsMet();
 }
Example #2
0
    /**
     * Renders the HTML.
     *
     * @wp-hook that_was_helpful
     *
     * @param int $post_id Optional. Post ID. Defaults to 0.
     *
     * @return void
     */
    public function render($post_id = 0)
    {
        if (!$post_id) {
            $post_id = get_the_ID();
            if (!$post_id) {
                return;
            }
        }
        $this->post->set_post_id($post_id);
        $this->state->set_active();
        $data = $this->post->get_data();
        ?>
		<div class="that-was-helpful that-was-helpful--<?php 
        echo $post_id;
        ?>
">
			<?php 
        if (is_user_logged_in()) {
            ?>
				<form action="" method="POST" class="that-was-helpful__form">
					<?php 
            $this->nonce->print_field();
            ?>
					<input type="hidden" name="vote_post" value="<?php 
            echo $post_id;
            ?>
">
					<?php 
            $class = 'that-was-helpful__submit button';
            if ($data->vote) {
                $class .= ' active';
                $title = $data->titles['active'];
            } else {
                $title = $data->titles['vote'];
            }
            ?>
					<input type="submit" name="vote"
						value="<?php 
            echo esc_attr_x('That was helpful', 'Vote button text', 'that-was-helpful');
            ?>
"
						class="<?php 
            echo $class;
            ?>
" title="<?php 
            echo $title;
            ?>
">
				</form>
			<?php 
        }
        ?>
			<span class="that-was-helpful__votes">
				<?php 
        $string = _nx('%d visitor found that helpful.', '%d visitors found that helpful.', $data->votes, 'Vote description', 'that-was-helpful');
        $string = esc_html($string);
        printf($string, $data->votes);
        ?>
			</span>
		</div>
		<?php 
    }