1
0
Fork 0
mirror of https://github.com/buddypress/buddypress.git synced 2026-07-24 21:16:56 +08:00
buddypress/tests/phpunit/testcases/core/class-bp-button.php
Mathieu Viet 89a6666dc6 Fully enjoy Yoast’s PHPUnit polyfills
Using these polyfills let us use PHPUnit v9.x for our tests and add PHP 8.1 to our testing matrix. Some additional edits to our PHP unit tests suite were needed:

- Stop using PHPunit deprecated functions.
- Rename some `BP_UnitTestCase` methods to use Yoast's polyfills.
- Edit the PHP Unit test GH action and also run this action on pull requests.
- Update some composer dependencies, remove the one about `phpunit/phpunit:^7.5` and add a new composer script to use PHPUnit v9.x. 

Props renatonascalves, rafiahmedd

Closes https://github.com/buddypress/buddypress/pull/13
Fixes #8649


git-svn-id: https://buddypress.svn.wordpress.org/trunk@13314 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
2022-08-13 08:58:51 +00:00

264 lines
6.2 KiB
PHP

<?php
/**
* @group core
* @group BP_Button
*/
class BP_Tests_BP_Button extends BP_UnitTestCase {
/**
* @group block_self
*/
public function test_block_self_own_profile() {
$u = self::factory()->user->create();
$this->set_current_user( $u );
$this->go_to( bp_core_get_user_domain( $u ) );
$b = new BP_Button( array(
'id' => 'foo',
'component' => 'members',
'block_self' => true,
) );
$this->assertEquals( '', $b->contents );
}
/**
* @group block_self
*/
public function test_block_self_others_profile() {
$u1 = self::factory()->user->create();
$this->set_current_user( $u1 );
$u2 = self::factory()->user->create();
$this->go_to( bp_core_get_user_domain( $u2 ) );
$b = new BP_Button( array(
'id' => 'foo',
'component' => 'members',
'block_self' => true,
) );
$this->assertNotEmpty( $b->contents );
}
/**
* @group block_self
*/
public function test_block_self_inside_members_loop() {
$now = time();
$u1 = self::factory()->user->create( array(
'last_activity' => date( 'Y-m-d H:i:s', $now ),
) );
$u2 = self::factory()->user->create( array(
'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
) );
$this->set_current_user( $u1 );
$found = array();
if ( bp_has_members() ) {
while ( bp_members() ) {
bp_the_member();
$b = new BP_Button( array(
'id' => 'foo',
'component' => 'members',
'block_self' => true,
) );
$found[ bp_get_member_user_id() ] = empty( $b->contents );
}
}
$expected = array(
$u1 => true,
$u2 => false,
);
$this->assertSame( $expected, $found );
// clean up
$GLOBALS['members_template'] = null;
}
/**
* @group block_self
*/
public function test_block_self_false_inside_members_loop() {
$now = time();
$u1 = self::factory()->user->create( array(
'last_activity' => date( 'Y-m-d H:i:s', $now ),
) );
$u2 = self::factory()->user->create( array(
'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
) );
$this->set_current_user( $u1 );
$found = array();
if ( bp_has_members() ) {
while ( bp_members() ) {
bp_the_member();
$b = new BP_Button( array(
'id' => 'foo',
'component' => 'members',
'block_self' => false,
) );
$found[ bp_get_member_user_id() ] = empty( $b->contents );
}
}
$expected = array(
$u1 => false,
$u2 => false,
);
$this->assertSame( $expected, $found );
// clean up
$GLOBALS['members_template'] = null;
}
/**
* @group block_self
*/
public function test_block_self_inside_members_loop_on_my_profile_page() {
$now = time();
$u1 = self::factory()->user->create( array(
'last_activity' => date( 'Y-m-d H:i:s', $now ),
) );
$u2 = self::factory()->user->create( array(
'last_activity' => date( 'Y-m-d H:i:s', $now - 100 ),
) );
$this->set_current_user( $u1 );
$this->go_to( bp_core_get_user_domain( $u1 ) );
$found = array();
if ( bp_has_members() ) {
while ( bp_members() ) {
bp_the_member();
$b = new BP_Button( array(
'id' => 'foo',
'component' => 'members',
'block_self' => true,
) );
$found[ bp_get_member_user_id() ] = empty( $b->contents );
}
}
$expected = array(
$u1 => true,
$u2 => false,
);
$this->assertSame( $expected, $found );
// clean up
$GLOBALS['members_template'] = null;
}
/**
* @ticket BP7226
*/
public function test_bp_button_new_args() {
$b = new BP_Button( array(
'id' => 'foo',
'component' => 'members',
'block_self' => false,
'must_be_logged_in' => false,
'parent_element' => 'section',
'parent_attr' => array(
'class' => 'section-class',
'id' => 'section-id',
'data-parent' => 'foo',
),
'button_element' => 'button',
'button_attr' => array(
'autofocus' => 'autofocus',
'type' => 'submit',
'name' => 'my-button'
)
) );
$this->assertIsInt( strpos( $b->contents, '<section ' ) );
$this->assertIsInt( strpos( $b->contents, 'class="section-class ' ) );
$this->assertIsInt( strpos( $b->contents, 'id="section-id"' ) );
$this->assertIsInt( strpos( $b->contents, 'data-parent="foo"' ) );
$this->assertIsInt( strpos( $b->contents, '<button ' ) );
$this->assertIsInt( strpos( $b->contents, 'autofocus="autofocus"' ) );
$this->assertIsInt( strpos( $b->contents, 'type="submit"' ) );
$this->assertIsInt( strpos( $b->contents, 'name="my-button"' ) );
}
/**
* @ticket BP7226
*/
public function test_bp_button_deprecated_args_should_still_render() {
$b = new BP_Button( array(
'id' => 'foo',
'component' => 'members',
'block_self' => false,
'must_be_logged_in' => false,
'wrapper' => 'section',
'wrapper_class' => 'section-class',
'wrapper_id' => 'section-id',
'link_href' => 'http://example.com',
'link_class' => 'link-class',
'link_id' => 'link-id',
'link_rel' => 'nofollow',
'link_title' => 'link-title'
) );
$this->assertIsInt( strpos( $b->contents, '<section ' ) );
$this->assertIsInt( strpos( $b->contents, 'class="section-class ' ) );
$this->assertIsInt( strpos( $b->contents, 'id="section-id"' ) );
$this->assertIsInt( strpos( $b->contents, 'href="http://example.com"' ) );
$this->assertIsInt( strpos( $b->contents, 'class="link-class"' ) );
$this->assertIsInt( strpos( $b->contents, 'id="link-id"' ) );
$this->assertIsInt( strpos( $b->contents, 'rel="nofollow"' ) );
$this->assertIsInt( strpos( $b->contents, 'title="link-title"' ) );
}
/**
* @ticket BP7226
*/
public function test_bp_button_new_element_attrs_have_precedence_over_deprecated_element_attrs() {
$b = new BP_Button( array(
'id' => 'foo',
'component' => 'members',
'block_self' => false,
'must_be_logged_in' => false,
'button_element' => 'button',
'button_attr' => array(
'class' => 'new-class',
),
'link_class' => 'old-class'
) );
$this->assertIsInt( strpos( $b->contents, '<button class="new-class"' ) );
}
/**
* @ticket BP7226
*/
public function test_bp_button_new_element_attrs_should_not_render_for_empty_attrs() {
$b = new BP_Button( array(
'id' => 'foo',
'component' => 'members',
'block_self' => false,
'must_be_logged_in' => false,
'button_element' => 'button',
'button_attr' => array(
'class' => '',
),
) );
$this->assertFalse( strpos( $b->contents, '<button class=""' ) );
}
}