1
0
Fork 0
mirror of https://github.com/buddypress/buddypress.git synced 2026-07-23 21:06:56 +08:00
buddypress/tests/phpunit/testcases/core/class-bp-component.php
Renato Alves 41802f7709 Include the V2 of the BP REST API in BuddyPress core.
We are officially deprecating the V1 of the BP REST API. And bundling the new, default, V2 of the BP REST API inside BuddyPress core. Previously, the V1 was developed as a plugin in a separate repo (https://github.com/buddypress/BP-REST).

- One of the main differences between the V1 and V2 is how objects are returned. Single items are no longer returned as an `array`;
- We have a new `BP_Test_REST_Controller_Testcase` for testing the new API endpoints;
- We changed the names of our controllers to follow our autoloader rules;
- Removed the BP-REST plugin from `wp-env` and from our release script;
- And we added notices for the deprecated V1 API (endpoints and files).

Props imath & I. ;)

Fixes #8200
See #9145
Closes https://github.com/buddypress/buddypress/pull/337

git-svn-id: https://buddypress.svn.wordpress.org/trunk@14026 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
2024-09-27 21:11:27 +00:00

171 lines
4.2 KiB
PHP

<?php
include_once BP_TESTS_DIR . '/assets/class-bptest-component.php';
/**
* @group core
* @group BP_Component
*/
class BP_Tests_BP_Component_TestCases extends BP_UnitTestCase {
/**
* @group bp_blocks
*/
public function test_component_block_globals() {
$expected = array(
'dynamic_widget_classname' => 'widget_example_classname',
);
$example = new BPTest_Component(
array(
'globals' => array(
'block_globals' => array(
'bp/example-block' => $expected,
)
),
)
);
do_action( 'bp_setup_globals' );
$this->assertEquals( $expected, $example->block_globals['bp/example-block']->props );
}
/**
* @group bp_rewrites
*/
public function test_component_rewrite_globals() {
$expected = array(
'directory' => 'bp_examples',
'single_item' => 'bp_example',
'single_item_action' => 'bp_example_action',
);
$example = new BPTest_Component(
array(
'globals' => array(
'rewrite_ids' => array(
'directory' => 'examples ',
'single_item' => 'Exam?ple',
'single_item_action' => 'bp_example_action',
)
),
)
);
do_action( 'bp_setup_globals' );
$this->assertEquals( $expected, $example->rewrite_ids );
}
/**
* @group bp_rewrites
*/
public function test_component_add_rewrite_tags() {
$example = new BPTest_Component(
array(
'globals' => array(
'rewrite_ids' => array(
'directory' => 'examples',
'directory_type' => 'example_type',
)
),
)
);
do_action( 'bp_setup_globals' );
$expected_directory_regex = '([1]{1,})';
$rewrite_tags = array(
'directory_type' => '([^/]+)',
);
$example->add_rewrite_tags( $rewrite_tags );
global $wp_rewrite;
$position = array_search( '%' . $example->rewrite_ids['directory'] . '%', $wp_rewrite->rewritecode, true );
$this->assertEquals( $wp_rewrite->rewritereplace[ $position ], $expected_directory_regex );
$position = array_search( '%' . $example->rewrite_ids['directory_type'] . '%', $wp_rewrite->rewritecode, true );
$this->assertEquals( $wp_rewrite->rewritereplace[ $position ], $rewrite_tags['directory_type'] );
}
/**
* @group bp_rewrites
*/
public function test_component_add_rewrite_rules() {
$example = new BPTest_Component(
array(
'globals' => array(
'root_slug' => 'examples',
'rewrite_ids' => array(
'directory' => 'examples',
'directory_type' => 'example_type',
'single_item' => 'example',
'single_item_component' => 'example_component',
)
),
)
);
do_action( 'bp_setup_globals' );
$rewrite_tags = array(
'directory_type' => '([^/]+)',
);
$example->add_rewrite_tags( $rewrite_tags );
$rewrite_rules = array(
'directory_type' => array(
'order' => 95,
'regex' => $example->root_slug . '/type/([^/]+)/?$',
'query' => 'index.php?' . $example->rewrite_ids['directory'] . '=1&' . $example->rewrite_ids['directory_type'] . '=$matches[1]',
),
);
$example->add_rewrite_rules( $rewrite_rules );
global $wp_rewrite;
$this->assertEquals( $wp_rewrite->extra_rules_top[ $rewrite_rules['directory_type']['regex'] ], $rewrite_rules['directory_type']['query'] );
}
/**
* @group bp_rewrites
*/
public function test_component_add_permastructs() {
$example = new BPTest_Component(
array(
'globals' => array(
'has_directory' => true,
'root_slug' => 'examples',
'rewrite_ids' => array(
'directory' => 'examples',
'example_signup' => 'signup',
)
),
)
);
do_action( 'bp_setup_globals' );
$expected = 'example-signup/%' . $example->rewrite_ids['example_signup'] . '%';
$permastructs = array(
$example->rewrite_ids['example_signup'] => array(
'permastruct' => $expected,
'args' => array(),
),
);
$example->add_permastructs( $permastructs );
global $wp_rewrite;
// The directory permastruct should be created automatically.
$this->assertTrue( isset( $wp_rewrite->extra_permastructs['bp_examples'] ) );
// The custom permastruct should be created as requested.
$this->assertEquals( $wp_rewrite->extra_permastructs[ $example->rewrite_ids['example_signup'] ]['struct'], $expected );
}
}