<commit_before><?php
/**
 * Tests for the plugin's admin UI.
 *
 * @package McAvoy
 * @author  <NAME>
 */

namespace McAvoy\Admin;

use McAvoy;
use Mockery;
use Patchwork as P;
use WP_Mock as M;

class AdminTest extends McAvoy\TestCase {

	protected $testFiles = array(
		'admin.php',
		'class-list-table.php',
		'core.php',
	);

	public function test_search_page_callback() {
		P\redefine('McAvoy\ListTable::prepare_items', function () {} );
		P\redefine('McAvoy\ListTable::display', function () {} );

		M::wpFunction( __NAMESPACE__ . '\maybe_delete_queries', array(
			'times'  => 1,
		) );

		M::wpFunction( 'get_bloginfo', array(
			'times'  => 1,
			'args'   => 'name',
			'return' => 'BLOG_NAME',
		) );

		M::wpFunction( 'current_user_can', array(
			'times'  => 1,
			'args'   => array( 'mcavoy_delete_queries' ),
			'return' => true,
		) );

		M::wpFunction( 'wp_nonce_field', array(
			'times'  => 1,
			'args'   => array( 'delete-queries', 'mcavoy-nonce' ),
		) );

		M::wpPassthruFunction( '__' );
		M::wpPassthruFunction( 'esc_html_e' );
		M::wpPassthruFunction( 'esc_html' );
		M::wpPassthruFunction( 'esc_attr_e' );

		ob_start();
		search_page_callback();
		$result = ob_get_contents();
		ob_end_clean();

		$this->assertContains( '<div class="wrap">', $result );

		P\restoreAll();
	}

	public function test_search_page_callback_checks_delete_cap() {
		P\redefine('McAvoy\ListTable::prepare_items', function () {} );
		P\redefine('McAvoy\ListTable::display', function () {} );

		M::wpFunction( __NAMESPACE__ . '\maybe_delete_queries', array(
			'times'  => 1,
		) );

		M::wpFunction( 'get_bloginfo', array(
			'return' => 'BLOG_NAME',
		) );

		M::wpFunction( 'current_user_can', array(
			'times'  => 1,
			'args'   => array( 'mcavoy_delete_queries' ),
			'return' => false,
		) );

		M::wpPassthruFunction( '__' );
		M::wpPassthruFunction( 'esc_html_e' );
		M::wpPassthruFunction( 'esc_html' );

		ob_start();
		search_page_callback();
		$result = ob_get_contents();
		ob_end_clean();

		$this->assertNotContains( '<form method="POST" id="mcavoy-delete-queries">', $result );

		P\restoreAll();
	}

	public function test_maybe_delete_queries() {
		$_POST['mcavoy-nonce'] = 'foo';

		$logger = Mockery::mock( 'McAvoy\Loggers\TestLogger' )->makePartial();
		$logger->shouldReceive( 'delete_queries' )->once();

		M::wpFunction( 'wp_verify_nonce', array(
			'times'  => 1,
			'args'   => array( 'foo', 'delete-queries' ),
			'return' => true,
		) );

		M::wpFunction( 'current_user_can', array(
			'times'  => 1,
			'args'   => array( 'mcavoy_delete_queries' ),
			'return' => true,
		) );

		M::wpFunction( 'McAvoy\get_logger', array(
			'times'  => 1,
			'return' => $logger,
		) );<commit_msg>Update tests that use Patchwork to support the newer method names<commit_after><?php
/**
 * Tests for the plugin's admin UI.
 *
 * @package McAvoy
 * @author  <NAME>
 */

namespace McAvoy\Admin;

use McAvoy;
use Mockery;
use Patchwork as P;
use WP_Mock as M;

class AdminTest extends McAvoy\TestCase {

	protected $testFiles = array(
		'admin.php',
		'class-list-table.php',
		'core.php',
	);

	public function test_search_page_callback() {
		P\replace('McAvoy\ListTable::prepare_items', function () {} );
		P\replace('McAvoy\ListTable::display', function () {} );

		M::wpFunction( __NAMESPACE__ . '\maybe_delete_queries', array(
			'times'  => 1,
		) );

		M::wpFunction( 'get_bloginfo', array(
			'times'  => 1,
			'args'   => 'name',
			'return' => 'BLOG_NAME',
		) );

		M::wpFunction( 'current_user_can', array(
			'times'  => 1,
			'args'   => array( 'mcavoy_delete_queries' ),
			'return' => true,
		) );

		M::wpFunction( 'wp_nonce_field', array(
			'times'  => 1,
			'args'   => array( 'delete-queries', 'mcavoy-nonce' ),
		) );

		M::wpPassthruFunction( '__' );
		M::wpPassthruFunction( 'esc_html_e' );
		M::wpPassthruFunction( 'esc_html' );
		M::wpPassthruFunction( 'esc_attr_e' );

		ob_start();
		search_page_callback();
		$result = ob_get_contents();
		ob_end_clean();

		$this->assertContains( '<div class="wrap">', $result );

		P\undoAll();
	}

	public function test_search_page_callback_checks_delete_cap() {
		P\replace('McAvoy\ListTable::prepare_items', function () {} );
		P\replace('McAvoy\ListTable::display', function () {} );

		M::wpFunction( __NAMESPACE__ . '\maybe_delete_queries', array(
			'times'  => 1,
		) );

		M::wpFunction( 'get_bloginfo', array(
			'return' => 'BLOG_NAME',
		) );

		M::wpFunction( 'current_user_can', array(
			'times'  => 1,
			'args'   => array( 'mcavoy_delete_queries' ),
			'return' => false,
		) );

		M::wpPassthruFunction( '__' );
		M::wpPassthruFunction( 'esc_html_e' );
		M::wpPassthruFunction( 'esc_html' );

		ob_start();
		search_page_callback();
		$result = ob_get_contents();
		ob_end_clean();

		$this->assertNotContains( '<form method="POST" id="mcavoy-delete-queries">', $result );

		P\undoAll();
	}

	public function test_maybe_delete_queries() {
		$_POST['mcavoy-nonce'] = 'foo';

		$logger = Mockery::mock( 'McAvoy\Loggers\TestLogger' )->makePartial();
		$logger->shouldReceive( 'delete_queries' )->once();

		M::wpFunction( 'wp_verify_nonce', array(
			'times'  => 1,
			'args'   => array( 'foo', 'delete-queries' ),
			'return' => true,
		) );

		M::wpFunction( 'current_user_can', array(
			'times'  => 1,
			'args'   => array( 'mcavoy_delete_queries' ),
			'return' => true,
		) );

		M::wpFunction( 'McAvoy\get_logger', array(
			'times'  => 1,
			'return' => $logger,
		) );
{'fraction_non_alphanumeric': 0.19078947368421054, 'fraction_numerical': 0.003289473684210526, 'mean_word_length': 4.12933458294283, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 63, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1852153', 'n_tokens_mistral': 2367, 'n_tokens_neox': 2103, 'n_words': 364}