public function setUp() { \WP_Mock::setUp(); \WP_Mock::wpFunction('stripslashes_deep', ['return' => function ($array) { return $array; }]); }
public function testSetupMetaBoxesNotPostOrPage() { \WP_Mock::wpFunction('add_meta_box', ['times' => 0]); \WP_Mock::onFilter('dxw_content_review_post_types')->with(['post', 'page'])->reply(['meow']); \Dxw_Content_Review\Dxw_Content_Review::setup_meta_boxes('post'); \Dxw_Content_Review\Dxw_Content_Review::setup_meta_boxes('page'); }
/** * Tests our ability to mock the cli interface * * @group wip * @covers taxonomy * */ function testTaxonomyWithExistingToAndFromExpectsTaxonomiesMigrated() { // arrange $args = $this->validTaxonomyArgs; $assoc_args = array('post_type' => 'post'); $this->posts['args'] = $args + $assoc_args; var_dump($this->posts); $mock = $this->getMockBuilder('\\CFPB\\Migrate_Command')->setMethods(array('get_specified_posts'))->getMock(); $mock->expects($this->once())->method('get_specified_posts')->will($this->returnValue($this->posts)); $new_term0 = array('slug' => 'term'); $new_term1 = array('slug' => 'another'); // mock taxonomy_exists form the WordPress API which we expect to be fired exactly twice \WP_Mock::wpFunction('taxonomy_exists', array('times' => 2, 'return' => true)); // mock wp_get_post_terms from the WordPress API which we expect to fire exactly twice $terms = \WP_Mock::wpFunction('wp_get_post_terms', array('times' => 2, 'with' => array(array($this->post0->ID, $args[0]), array($this->post1->ID, $args[0])), 'return' => $this->terms)); // mock wp_insert_term from the WordPress API which we expect to fire exactly twice $new_term = \WP_Mock::wpFunction('wp_insert_term', array('times' => 2, 'with' => array(array($this->term0->name, $args[1], array('slug' => $this->term0->slug))), 'return' => array('term_id' => 0, 'term_id' => 1))); // we expect get_term to fire twice with \WP_Mock::wpFunction('get_term', array('times' => 2, 'with' => array(array(1, $args[1])), 'return' => $this->term0)); // we expect wp_set_objec_terms to fire twice \WP_Mock::wpFunction('wp_set_object_terms', array('times' => 2, 'return' => true)); $cli = $this->cli->getMock(); $cli->expects($this->once())->method('success')->will($this->returnValue('Success')); // act $action = $mock->taxonomy($args, $assoc_args); $this->assertTrue($action == 'Success'); }
public function test_load_message_scenarios() { $defaults = array('days' => 30, 'limit' => 50); $_GET = array('nonce' => true); $_POST = $defaults; $instance = Mockery::mock('RevisionStrike')->makePartial(); $instance->shouldReceive('strike')->with($defaults); $instance->shouldReceive('get_stats')->times(3)->andReturn(array('count' => 0), array('count' => 5, 'deleted' => 0), array('count' => 5, 'deleted' => 5)); M::wpFunction('wp_verify_nonce', array('return' => true)); M::wpFunction('_n', array('times' => 1, 'return' => '%d post revisions')); M::wpPassthruFunction('wp_nonce_url'); M::wpPassthruFunction('submit_button'); // Pass 1: no revisions found ob_start(); include $this->tools_file; $result = ob_get_contents(); ob_end_clean(); $this->assertContains('<div class="error"><p>', $result, 'No revisions found did not trigger an error message'); // Pass 2: revisions found, but nothing deleted ob_start(); include $this->tools_file; $result = ob_get_contents(); ob_end_clean(); $this->assertContains('<div class="error"><p>', $result, 'The user should be warned when revisions were found be we can\'t delete them'); // Pass 3: revisions deleted successfully ob_start(); include $this->tools_file; $result = ob_get_contents(); ob_end_clean(); $this->assertContains('<div class="updated"><p>', $result, 'Notify the user when revisions are deleted'); $this->assertContains('5 post revisions', $result, 'Display the number of revisions that were removed.'); unset($_GET, $_POST); }
public function test_customize_meta_boxes() { $testee = new Testee(); WP_Mock::wpFunction('remove_meta_box', array('times' => 1, 'args' => array(WP_Mock\Functions::type('string'), WP_Mock\Functions::type('string'), WP_Mock\Functions::type('string')))); $testee->customize_meta_boxes(); $this->assertConditionsMet(); }
public function test_content_type() { $plugin = new Plugin(); \WP_Mock::wpFunction('register_post_type', array('times' => 3, 'arg' => array('lnh_matches'))); \WP_Mock::wpFunction('get_option', array('times' => 1, 'arg' => array('lnh_equipe_handball'), 'return' => array('equipe' => 'toulouse', 'classement' => '1', 'joueurs' => '1', 'calendrier' => '1'))); $plugin->createContentType(); }
public function testToString() { $layout = new \Dxw\Iguana\Theme\Layout(); $layout->slug = 'slug'; \WP_Mock::onFilter('roots_wrap_slug')->with(['layouts/main.php'])->reply(['layouts/my-layout.php']); \WP_Mock::wpFunction('locate_template', ['args' => [['layouts/my-layout.php']], 'return' => 'correct output']); $this->assertEquals('correct output', $layout->__toString()); }
public function test_checkAjaxReferer() { $action = 'nonce_action'; $query_arg = '_wpnonce_name'; $myNonce = new WPnonce($action); \WP_Mock::wpFunction('check_ajax_referer', array('times' => 1, 'args' => array($myNonce->getAction(), $query_arg, true), 'return' => true)); $this->assertTrue($myNonce->checkAjaxReferer($query_arg, true)); }
public function test_meta_box_save_rejects_invalid_before_or_after_setting() { $cpt = new CPT(); WP_Mock::wpFunction('wp_verify_nonce', array('times' => 1, 'args' => array('nonce-value', 'itelic-renewal-reminders-metabox'), 'return' => true)); WP_Mock::wpFunction('update_post_meta', array('times' => 1, 'args' => array(1, '_itelic_renewal_reminder_days', 3), 'return' => true)); WP_Mock::wpFunction('update_post_meta', array('times' => 1, 'args' => array(1, '_itelic_renewal_reminder_boa', 'before'), 'return' => true)); $cpt->do_save(1, array('itelic_reminder_nonce' => 'nonce-value', 'itelic_reminder' => array('days' => 3, 'boa' => 'garbage'))); }
/** * Test activation routine. */ public function test_activate() { // Setup \WP_Mock::wpFunction('flush_rewrite_rules', array('times' => 1)); // Act activate(); // Verify $this->assertConditionsMet(); }
/** * @covers tfrommen\ThatWasHelpful\Controllers\Update::update * @dataProvider provide_update_data * * @param bool $expected * @param string $version * @param string $old_version * * @return void */ public function test_update($expected, $version, $old_version) { $testee = new Testee($version); WP_Mock::wpFunction('get_option', array('times' => 1, 'args' => array(Mockery::type('string')), 'return' => $old_version)); if ($old_version !== $version) { WP_Mock::wpFunction('update_option', array('times' => 1, 'args' => array(Mockery::type('string'), $version))); } $this->assertSame($expected, $testee->update()); $this->assertConditionsMet(); }
public function test_interval_after() { $post = new WP_Post((object) array('post_type' => Reminder\CPT::TYPE, 'ID' => 1)); WP_Mock::wpFunction('get_post_meta', array('times' => 1, 'args' => array(1, '_itelic_renewal_reminder_days', true), 'return' => 5)); WP_Mock::wpFunction('get_post_meta', array('times' => 1, 'args' => array(1, '_itelic_renewal_reminder_boa', true), 'return' => Reminder::TYPE_AFTER)); $reminder = new Reminder($post); $expects = new DateInterval('P5D'); $expects->invert = true; $this->assertEquals($expects, $reminder->get_interval()); }
protected function plugin() { // functions used as part of constructor Mock::wpPassthruFunction('untrailingslashit'); Mock::wpFunction('plugin_dir_path', array('args' => bootstrap()->get_framework_path() . '/woocommerce/class-sv-wc-plugin.php', 'return' => bootstrap()->get_framework_path() . '/woocommerce')); Mock::wpFunction('is_admin', array('return' => true)); Mock::wpPassthruFunction('plugin_basename'); Mock::wpFunction('has_action', array('return' => true)); $args = array('mock', '7.7.7', 'woocommerce-mock', 'args' => array('dependencies' => 'json', 'function_dependencies' => 'ftp_ssl_connect')); return $this->getMockBuilder('SV_WC_Plugin')->setConstructorArgs($args)->getMockForAbstractClass(); }
public function setUp() { \WP_Mock::setUp(); \WP_Mock::wpFunction('stripslashes_deep', ['return' => function ($array) { $newArray = []; foreach ($array as $k => $v) { $newArray[$k] = stripslashes($v); } return $newArray; }]); }
public function test_not_valid() { $query = Mockery::mock('WP_Query'); $query->in_the_loop = true; $query->shouldReceive('have_posts')->once()->andReturn(false); $query->shouldReceive('rewind_posts')->once(); $query->shouldReceive('the_post')->never(); WP_Mock::wpFunction('wp_reset_postdata', array('times' => 1)); $object = new Loopable_Query($query); $this->assertFalse($object->valid(), 'valid() did not return true when have_posts() did!'); }
public function testWpHead() { $useAtom = new \Dxw\Iguana\Extras\UseAtom(); \WP_Mock::wpFunction('get_bloginfo', ['args' => ['name'], 'return' => 'Xyz']); \WP_Mock::wpFunction('esc_attr', ['return' => function ($a) { return '_' . $a . '_'; }]); \WP_Mock::wpFunction('get_feed_link', ['args' => ['atom'], 'return' => 'xyz']); $this->expectOutputString(' <link rel="alternate" type="application/atom+xml" title="_Xyz_ Feed" href="_xyz_">' . "\n "); $useAtom->wpHead(); }
function testMissingKeyMaybeFlushRulesExpectsFlushRewriteRulesCalledOnce() { // Arrange $postTypeObj = new PostTypeObj(); \WP_Mock::wpFunction('flush_rewrite_rules', array('times' => 1, 'with' => array(true))); global $wp_rewrite; $wp_rewrite->extra_rules_top = array(); // Act $postTypeObj->maybe_flush_rewrite_rules('key'); // Assert }
public function test_nonce_field() { \WP_Mock::wpFunction('wp_create_nonce', array('times' => 1)); $nonce = new Nonce(); $html = $nonce->getDisplay(); $this->assertRegExp('/type=\\"hidden\\"/', $html); $this->assertRegExp('/name=\\"custom_meta_box_nonce\\"/', $html); $nonce->setValue('test'); $html_new = $nonce->getDisplay(); $this->assertEquals($html, $html_new); }
public function test_save_input_value_null() { $name = 'nom'; $value = ''; $id = 'id_form_test'; $post = new \stdClass(); $post->ID = 42; $checkbox = new Checkbox($name, $value, $id); \WP_Mock::wpFunction('get_post', array('times' => 1, 'arg' => array($post->ID), 'return' => $post)); \WP_Mock::wpFunction('update_post_meta', array('times' => 1, 'arg' => array($post->ID, $id, $value), 'return' => 4)); $this->assertNotFalse($checkbox->save($post->ID)); }
public function testCreation() { $user = new stdClass(); $user->ID = 1; WP_Mock::wpFunction('wp_get_current_user', ['times' => 2, 'return' => $user]); WP_Mock::wpFunction('wp_salt', ['args' => ['nonce'], 'times' => 2, 'return' => 'salt']); WP_Mock::wpFunction('wp_get_session_token', ['times' => 2, 'return' => 'session-1']); $nonce = wp_create_nonce(); $this->assertEquals(self::$nonce, $nonce); $nonce = wp_create_nonce('action'); $this->assertEquals(self::$actionNonce, $nonce); }
public function testSaveWithErrors() { $this->subject->setValue(''); $validator = new Validator\Required(['message' => 'my_field is required!']); $this->subject->attachValidator($validator); $this->assertFalse($this->subject->save()); $this->assertEquals(['my_field is required!'], $this->subject->getErrors()); WP::wpFunction('update_post_meta', ['args' => [$this->postId, $this->name, 'foo'], 'times' => 1, 'return' => 12345]); $this->subject->setValue('foo'); $this->assertEquals(12345, $this->subject->save()); $this->assertEquals([], $this->subject->getErrors()); }
private function make_request() { $url = 'http://acme.org/new-authz'; $response = MockData::get_head_response(); \WP_Mock::setUp(); // Mock the remote request \WP_Mock::wpFunction('wp_remote_request', array('args' => array($url, array('body' => '')), 'times' => 1, 'return' => $response)); $nonce = new \LEWP\Request\Nonce($url); $nonce->send(); \WP_Mock::tearDown(); return $nonce; }
/** * @covers tfrommen\ThatWasHelpful\Models\TextDomain::load * * @return void */ public function test_load() { $file = '/path/to/file.php'; $domain = 'text-domain'; $path = '/domain'; WP_Mock::wpPassthruFunction('plugin_basename', array('times' => 1, 'args' => array(WP_Mock\Functions::type('string')))); $testee = new Testee($file, $domain, $path); $path = dirname($file) . $path; WP_Mock::wpFunction('load_plugin_textdomain', array('times' => 1, 'args' => array($domain, FALSE, $path))); $testee->load(); $this->assertConditionsMet(); }
public function test_exception_thrown_if_invalid_key_type() { $options = array('a' => 'b'); $product = $this->getMockBuilder('\\ITELIC\\Product')->disableOriginalConstructor()->getMock(); $product->method('get_feature')->willReturnMap(array(array('licensing', array('field' => 'key-type'), 'type'), array('licensing', array('field' => 'type.type'), $options))); $customer = $this->getMockBuilder('\\IT_Exchange_Customer')->disableOriginalConstructor()->getMock(); $transaction = $this->getMockBuilder('\\IT_Exchange_Transaction')->disableOriginalConstructor()->getMock(); WP_Mock::wpFunction('itelic_get_key_type_generator', array('times' => 1, 'args' => array('type'), 'return' => null)); $factory = new \ITELIC\Key\Factory($product, $customer, $transaction); $this->setExpectedException('\\UnexpectedValueException'); $factory->make(); }
public function test_serialize() { $notification = $this->getMock('\\IronBound\\WP_Notifications\\Contract'); $notification->expects($this->once())->method('serialize')->willReturn(serialize(array('test' => 'data'))); $txn = $this->getMockBuilder('\\IT_Exchange_Transaction')->disableOriginalConstructor()->getMock(); $txn->ID = 1; WP_Mock::wpFunction('it_exchange_get_transaction', array('times' => 1, 'args' => array(1), 'return' => $txn)); $guest = new \ITELIC\Utils\Guest_Notification($notification, $txn); $serialized = serialize($guest); $unserialzed = unserialize($serialized); $this->assertEquals($guest, $unserialzed); }
/** * @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(); }
public function testHydrate() { $instance = $this->getInstanceWithTrait(['foo' => 'text', 'bar' => ['type' => 'text'], 'baz' => 'textarea', 'qux' => 'wysiwyg']); $instance->foo = null; $instance->bar = null; $meta = ['foo' => 'I AM FOOOOO', 'bar' => 'I AM BARRRR']; WP::wpFunction('get_post_meta', ['args' => [$this->postId], 'times' => 1, 'return' => $meta]); $instance->hydrate(); $this->assertEquals('I AM FOOOOO', $instance->get('foo')); $this->assertEquals('I AM BARRRR', $instance->get('bar')); $this->assertEquals('', $instance->get('baz')); }
public function test_add_defer_forscript() { \WP_Mock::wpFunction('is_admin', array('return_in_order' => array(false, false, true))); $url = 'http://dev.otmoffice.com/wp-content/themes/otm/js/myscript.js?ver=1'; $this->assertEquals("http://dev.otmoffice.com/wp-content/themes/otm/js/myscript.js?ver=1", add_defer_forscript($url)); $url = 'http://dev.otmoffice.com/wp-content/themes/otm/js/myscript.js?ver=1#deferload'; $this->assertEquals("http://dev.otmoffice.com/wp-content/themes/otm/js/myscript.js?ver=1' defer='defer", add_defer_forscript($url)); $url = 'http://dev.otmoffice.com/wp-content/themes/otm/js/myscript.js #deferload'; $this->assertEquals("http://dev.otmoffice.com/wp-content/themes/otm/js/myscript.js' defer='defer", add_defer_forscript($url)); $url = 'http://dev.otmoffice.com/wp-content/themes/otm/js/myscript.js#deferload'; $this->assertEquals("http://dev.otmoffice.com/wp-content/themes/otm/js/myscript.js", add_defer_forscript($url)); }
public function test_submit_full_admin_email() { global $WPCF7_Submission_mock; $WPCF7_Submission_mock = $mock = $this->mock()->get_posted_data(array("test-key" => "something"))->new(); \WP_Mock::wpFunction('get_option', array('called' => 1, 'args' => array('pluginGCF7GHQ_settings'), 'return' => array('cf7ghq_none' => true))); \WP_Mock::wpFunction('get_option', array('called' => 1, 'args' => array('admin_email'), 'return' => "*****@*****.**")); \WP_Mock::wpFunction('wp_mail', array('called' => 1, 'args' => array(null, "Test: admin@domain.tld", "something"))); \WP_Mock::wpFunction('remove_filter', array('called' => 1)); $plugin = new CF7GHQ(); $mock = $mock = $this->mock()->in_demo_mode(false)->additional_setting(false)->title("Test")->prop("[input test-key something]")->new(); $plugin->submitForm($mock); }
private function get_directory_object() { $url = 'http://acme.org/directory'; $args = array('body' => ''); $response = MockData::get_directory_response(); \WP_Mock::setUp(); // Mock the remote request \WP_Mock::wpFunction('wp_remote_request', array('args' => array($url, $args), 'times' => 1, 'return' => $response)); $directory = new \LEWP\Request\Directory($url); $directory->send(); \WP_Mock::tearDown(); return $directory; }