/** * A wrapper for wpFunction that will simply set/override the return to be * a function that returns the value that its passed. For example, esc_attr * may need to be mocked, and it must return some value. wpPassthruFunction * will set esc_attr to return the value its passed. * * \WP_Mock::wpPassthruFunction( 'esc_attr' ); * echo esc_attr( 'some_value' ); // echoes "some_value" * * @param string $function_name * @param array $arguments */ public static function wpPassthruFunction($function_name, $arguments = array()) { $arguments = (array) $arguments; $arguments['return'] = function ($param) { return $param; }; self::$function_manager->register($function_name, $arguments); }
/** * Testing the post meta save by checking that all of the the methods are being called. * * If WP_Mock doesn't complain then then the test passes. */ function test_save_post() { //Fake the post data $_POST = array('data' => 'meta-value'); \WP_Mock::wpFunction('current_user_can', array('times' => 1, 'args' => array('edit_post'), 'return' => true)); \WP_Mock::wpFunction('sanitize_text_field', array('times' => 1, 'args' => array(\WP_Mock\Functions::type('string')), 'return' => 'meta-value')); \WP_Mock::wpFunction('update_post_meta', array('times' => 1, 'args' => array(\WP_Mock\Functions::type('int'), 'meta-name', \WP_Mock\Functions::type('string')), 'return' => true)); $this->instance->action_save_post(12345); }
public function test_key_created_when_generating_keys_for_transaction() { $mock_txn = $this->getMockBuilder('\\IT_Exchange_Transaction')->disableOriginalConstructor()->getMock(); $mock_txn->method('get_products')->willReturn(array(array('product_id' => 1))); $mock_product = $this->getMockBuilder('\\ITELIC\\Product')->disableOriginalConstructor()->getMock(); $mock_product->method('has_feature')->willReturn(true); $mock_customer = $this->getMockBuilder('\\IT_Exchange_Customer')->disableOriginalConstructor()->getMock(); WP_Mock::wpFunction('itelic_get_product', array('times' => 1, 'args' => array(1), 'return' => $mock_product)); WP_Mock::wpFunction('it_exchange_get_transaction_customer', array('times' => 1, 'args' => array($mock_txn), 'return' => $mock_customer)); WP_Mock::wpFunction('ITELIC\\generate_key_for_transaction_product', array('times' => 1, 'args' => array($mock_txn, $mock_product, \WP_Mock\Functions::type('ITELIC\\Key\\Factory'), ''), 'return' => true)); $this->assertTrue(\ITELIC\generate_keys_for_transaction($mock_txn)); }
/** * The get_staff_email_list function uses WP core functions so * we need to setup pass through methods using WP_Mock in order test it */ function test_get_staff_email_list() { \WP_Mock::wpFunction('get_post_meta', array('times' => 1, 'args' => array(\WP_Mock\Functions::type('int'), 'email_list', true), 'return' => array('*****@*****.**'))); // We use absint so we have to mock it as well \WP_Mock::wpFunction('absint', array('times' => 1, 'args' => array('*'), 'return' => 1)); //now we can call the method with a dummy value. $results = \tenup\demo\get_staff_email_list(1); //now that we have the data that we need, we can do any assertions //assert the type $this->assertSame('array', gettype($results)); //assert that the array has the email in it. $this->assertContains('*****@*****.**', $results); }
public function test_single_post_keys() { $wp_query = $this->getMockBuilder('WP_Query')->setMethods(array('get_queried_object', 'get', 'is_single', 'is_preview', 'is_page', 'is_archive', 'is_date', 'is_year', 'is_month', 'is_day', 'is_time', 'is_author', 'is_category', 'is_tag', 'is_tax', 'is_search', 'is_feed', 'is_comment_feed', 'is_trackback', 'is_home', 'is_404', 'is_comments_popup', 'is_paged', 'is_admin', 'is_attachment', 'is_singular', 'is_robots', 'is_posts_page', 'is_post_type_archive'))->getMock(); $template_type = 'single'; $wp_query->expects($this->any())->method('is_single')->will($this->returnValue(true)); $wp_query->post = $this->getMockBuilder('WP_Post')->getMock(); $wp_query->post->ID = 10; $wp_query->post->post_author = 23; $wp_query->posts = array(clone $wp_query->post); \WP_Mock::wpFunction('get_taxonomies', array('args' => array(), 'times' => 1, 'return' => array('category' => 'category', 'post_tag' => 'post_tag', 'nav_menu' => 'nav_menu', 'link_category' => 'link_category', 'post_format' => 'post_format'))); $categories = array((object) array('term_id' => '4', 'name' => 'Ham', 'slug' => 'ham', 'term_group' => '', 'term_taxonomy_id' => '23', 'taxonomy' => 'category', 'description' => '', 'parent' => '', 'count' => '64'), (object) array('term_id' => '1', 'name' => 'Bologne', 'slug' => 'bologne', 'term_group' => '', 'term_taxonomy_id' => '25', 'taxonomy' => 'category', 'description' => '', 'parent' => '', 'count' => '10')); $post_tag = array((object) array('term_id' => '4', 'name' => 'Ham', 'slug' => 'ham', 'term_group' => '', 'term_taxonomy_id' => '23', 'taxonomy' => 'post_tag', 'description' => '', 'parent' => '', 'count' => '64'), (object) array('term_id' => '1', 'name' => 'Bologne', 'slug' => 'bologne', 'term_group' => '', 'term_taxonomy_id' => '25', 'taxonomy' => 'post_tag', 'description' => '', 'parent' => '', 'count' => '10')); \WP_Mock::wpFunction('get_queried_object', array('args' => array(), 'times' => 5, 'return' => $wp_query->post)); \WP_Mock::wpFunction('get_the_terms', array('args' => array(\WP_Mock\Functions::type('int'), \WP_Mock\Functions::type('string')), 'times' => 5, 'return_in_order' => array($categories, $post_tag, false, false, false))); $object = new Purgely_Surrogate_Key_Collection($wp_query); $object->set_key('test-key'); $expected_keys = array(0 => 'post-' . $wp_query->post->ID, 1 => 'template-' . $template_type, 2 => 'category-' . $categories[0]->slug, 3 => 'category-' . $categories[1]->slug, 4 => 'post_tag-' . $post_tag[0]->slug, 5 => 'post_tag-' . $post_tag[1]->slug, 6 => 'author-' . $wp_query->post->post_author, 7 => 'test-key'); $this->assertEquals($expected_keys, $object->get_keys()); }
/** * @covers tfrommen\DefaultPostDate\SettingsField\View::render * * @return void */ public function test_render() { $option_value = '1984-05-02'; $option_name = 'option_name'; /** @var Option $option */ $option = Mockery::mock('tfrommen\\DefaultPostDate\\Setting\\Option')->shouldReceive('get')->andReturn($option_value)->shouldReceive('get_name')->andReturn($option_name)->getMock(); $testee = new Testee($option); WP_Mock::wpPassthruFunction('esc_html__', array('args' => array(WP_Mock\Functions::type('string'), 'default-post-date'))); WP_Mock::wpPassthruFunction('esc_attr', array('args' => array($option_value))); $output = <<<'HTML' <input type="text" id="default-post-date" name="%s" value="%s" maxlength="10" placeholder="YYYY-MM-DD"> <p class="description"> Please enter the default post date according to the <code>YYYY-MM-DD</code> date format. </p> HTML; $output = sprintf($output, $option_name, $option_value); $this->expectOutputString($output); $testee->render(); $this->assertConditionsMet(); }
public function test_call_wordpress_functions() { $this->submenu->capability('editor')->content('Simple Content'); \WP_Mock::wpFunction('add_submenu_page', ['args' => [\WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('callable')], 'times' => 1]); $this->submenu->add_menu(); }
public function testActivateOptionsNotPresent() { $plugin = new PassiveIndexationCheck(); \WP_Mock::wpFunction('get_option', array('times' => 1, 'args' => array('passive_indexation_check_settings'), 'return' => false)); \WP_Mock::wpFunction('get_option', array('times' => 1, 'args' => array('passive_indexation_check_emails'), 'return' => false)); \WP_Mock::wpFunction('update_option', array('times' => 1, 'args' => array('passive_indexation_check_settings', '*'))); \WP_Mock::wpFunction('update_option', array('times' => 1, 'args' => array('passive_indexation_check_emails', '*'))); \WP_Mock::wpFunction('wp_schedule_event', array('times' => 1, 'args' => array(\WP_Mock\Functions::type('long'), \WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('string')))); $plugin->activatePlugin(); }
/** * Tests whether post_type exists is called once * * @group stable * @group generate **/ function testPostTypeExistsCheckPostTypeExpectsPostTypeNameReturned() { // Arrange \WP_Mock::wpFunction('sanitize_key', array('times' => 1, 'return' => 'post')); \WP_Mock::wpFunction('post_type_exists', array('times' => 1, 'arg' => array(\WP_Mock\Functions::type('string')), 'return' => true)); $form = new TestValidBox(); $expected = 'post'; // act $actual = $form->check_post_type($form->post_type); // Assert $this->assertEquals($expected, $actual, 'Post type did not verify correctly'); }
public function test_revisionstrike_register_cron() { M::wpFunction('wp_next_scheduled', array('times' => 1, 'args' => array(RevisionStrike::STRIKE_ACTION), 'return' => false)); M::wpFunction('wp_schedule_event', array('times' => 1, 'args' => array(M\Functions::type('int'), 'daily', RevisionStrike::STRIKE_ACTION))); \revisionstrike_register_cron(); }
public function test_purge_related_result_when_one_request_produces_a_wp_error() { $url = 'http://example.com/2015/09/my-url'; $object = $this->setup_standard_collection($url); // We are going to issue a number of remote requests, which need some mocking \WP_Mock::wpFunction('wp_remote_request', array('args' => array(\WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('array')), 'times' => 14, 'return' => MockData::purge_url_response_200())); \WP_Mock::wpFunction('wp_remote_request', array('args' => array(\WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('array')), 'times' => 1, 'return' => MockData::purge_url_response_405())); \WP_Mock::wpFunction('is_wp_error', array('args' => array('*'), 'times' => 14, 'return' => false)); \WP_Mock::wpFunction('is_wp_error', array('args' => array('*'), 'times' => 1, 'return' => true)); \WP_Mock::wpFunction('wp_remote_retrieve_response_code', array('args' => array('*'), 'times' => 14, 'return' => '200')); $object->purge_related(); $result = $object->get_result(); $this->assertEquals('failure', $result); }
public function test_call_wordpress_functions() { $this->menu->icon('dashicons-menu')->capability('editor'); \WP_Mock::wpFunction('add_menu_page', ['args' => [\WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('callable'), \WP_Mock\Functions::type('string'), \WP_Mock\Functions::type('numeric')], 'times' => 1]); $this->menu->add_menu(); }