[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/tests/phpunit/testcases/common/ -> formatting.php (source)

   1  <?php
   2  
   3  /**
   4   * @group common
   5   * @group functions
   6   * @group bbp_make_clickable
   7   */
   8  class BBP_Tests_Common_Functions_Make_Clickable extends BBP_UnitTestCase {
   9  
  10      /**
  11       * @group bbp_make_clickable_misses
  12       * @group bbp_make_clickable_single
  13       * @covers ::bbp_make_clickable
  14       */
  15  	public function test_bbp_make_clickable_single_mention_misses() {
  16          $u1 = $this->factory->user->create( array(
  17              'user_login'    => 'foobarbaz',
  18              'user_nicename' => 'foobarbaz',
  19          ) );
  20  
  21          add_filter( 'bbp_make_mentions_clickable_classes', '__return_empty_array' );
  22  
  23          // Create the link to the user's profile
  24          $user   = get_userdata( $u1 );
  25          $url    = bbp_get_user_profile_url( $user->ID );
  26          $anchor = '<a href="%1$s" class="">@%2$s</a>';
  27          $name   = $user->user_nicename;
  28          $link   = sprintf( $anchor, esc_url( $url ), esc_html( $name ) );
  29          $link   = bbp_rel_nofollow( $link );
  30  
  31          // mentions inside links, should not be replaced
  32          $text                    = "Send messages to <a href='mailto:mail@%s.com'>Foo Bar Baz</a>";
  33          $at_name_in_mailto       = sprintf( $text, $name );
  34          $at_name_in_mailto_final = sprintf( $text, $name );
  35          $this->assertEquals( $at_name_in_mailto_final, bbp_make_clickable( $at_name_in_mailto ) );
  36  
  37          // mentions inside links, should not be replaced
  38          $text                  = "Send messages to <a href='@%s'>Foo Bar Baz</a>";
  39          $at_name_in_href       = sprintf( $text, $name );
  40          $at_name_in_href_final = sprintf( $text, $name );
  41          $this->assertEquals( $at_name_in_href_final, bbp_make_clickable( $at_name_in_href ) );
  42  
  43          // mentions inside links (with an external match) should not be replaced inside href ever
  44          $at_name_in_anchor       = sprintf( "Send messages to <a href='@%s'>@%s</a>", $name, $name );
  45          $at_name_in_anchor_final = sprintf( "Send messages to <a href='@%s'>@%s</a>", $name, $name );
  46          $this->assertEquals( $at_name_in_anchor_final, bbp_make_clickable( $at_name_in_anchor ) );
  47  
  48          // mentions inside links (with an external match) should not be replaced inside href ever
  49          $at_name_in_anchor_matched       = sprintf( "Send messages to <a href='@%s'>@%s</a> @%s", $name, $name, $name );
  50          $at_name_in_anchor_matched_final = sprintf( "Send messages to <a href='@%s'>@%s</a> %s", $name, $name, $link );
  51          $this->assertEquals( $at_name_in_anchor_matched_final, bbp_make_clickable( $at_name_in_anchor_matched ) );
  52  
  53          // mentions inside attributes, should not be replaced
  54          $text                  = '<a href=" @%s ................................ @%s @%s">@%s</a>';
  55          $at_name_in_attr       = sprintf( $text, $name, $name, $name, $name );
  56          $at_name_in_attr_final = sprintf( $text, $name, $name, $name, $name );
  57          $this->assertEquals( $at_name_in_attr_final, bbp_make_clickable( $at_name_in_attr ) );
  58  
  59          // mentions hugged by brackets, should not be replaced
  60          $text                    = "<@%s>";
  61          $at_name_in_hugged       = sprintf( $text, $name );
  62          $at_name_in_hugged_final = sprintf( $text, $name );
  63          $this->assertEquals( $at_name_in_hugged_final, bbp_make_clickable( $at_name_in_hugged ) );
  64  
  65          // mentions between brackets, should not be replaced even when linked after
  66          $at_name_between       = sprintf( "foo < %s > @%s", $name, $link );
  67          $at_name_between_final = sprintf( "foo < %s > @%s", $name, $link );
  68          $this->assertEquals( $at_name_between_final, bbp_make_clickable( $at_name_between ) );
  69  
  70          remove_filter( 'bbp_make_mentions_clickable_classes', '__return_empty_array' );
  71      }
  72  
  73      /**
  74       * @group bbp_make_clickable_hits
  75       * @group bbp_make_clickable_single
  76       * @covers ::bbp_make_clickable
  77       */
  78  	public function test_bbp_make_clickable_single_mention_hits() {
  79          $u1 = $this->factory->user->create( array(
  80              'user_login'    => 'foobarbaz',
  81              'user_nicename' => 'foobarbaz',
  82          ) );
  83  
  84          add_filter( 'bbp_make_mentions_clickable_classes', '__return_empty_array' );
  85  
  86          // Create the link to the user's profile
  87          $user   = get_userdata( $u1 );
  88          $url    = bbp_get_user_profile_url( $user->ID );
  89          $anchor = '<a href="%1$s" class="">@%2$s</a>';
  90          $name   = $user->user_nicename;
  91          $link   = sprintf( $anchor, esc_url( $url ), esc_html( $user->user_nicename ) );
  92          $link   = bbp_rel_nofollow( $link );
  93  
  94          // mentions inside links (with an external match) should not be replaced inside href ever
  95          $at_name_in_href_matched       = sprintf( "Send messages to <a href='@%s'>Foo Bar Baz</a>@%s @%s", $name, $name, $name );
  96          $at_name_in_href_matched_final = sprintf( "Send messages to <a href='@%s'>Foo Bar Baz</a>%s %s", $name, $link, $link );
  97          $this->assertEquals( $at_name_in_href_matched_final, bbp_make_clickable( $at_name_in_href_matched ) );
  98  
  99          // mentions inside linked text (with an external match) should not be linked
 100          $at_name_in_link_matched       = sprintf( "<a href='https://twitter.com/%s'>@%s</a>@%s @%s", $name, $name, $name, $name );
 101          $at_name_in_link_matched_final = sprintf( "<a href='https://twitter.com/%s'>@%s</a>%s %s", $name, $name, $link, $link );
 102          $this->assertEquals( $at_name_in_link_matched_final, bbp_make_clickable( $at_name_in_link_matched ) );
 103  
 104          // mentions after greater-than bracket, should be replaced
 105          $at_name_after_greater_than       = sprintf( "foo > @%s", $name );
 106          $at_name_after_greater_than_final = sprintf( "foo > %s", $link );
 107          $this->assertEquals( $at_name_after_greater_than_final, bbp_make_clickable( $at_name_after_greater_than ) );
 108  
 109          // mentions after less-than bracket, should be replaced
 110          $at_name_after_less_than       = sprintf( "foo < @%s", $name );
 111          $at_name_after_less_than_final = sprintf( "foo < %s", $link );
 112          $this->assertEquals( $at_name_after_less_than_final, bbp_make_clickable( $at_name_after_less_than ) );
 113  
 114          // mentions at end of normal text, should be replaced
 115          $at_name_at_end       = sprintf( 'Hello @%s', $name );
 116          $at_name_at_end_final = sprintf( 'Hello %s', $link );
 117          $this->assertEquals( $at_name_at_end_final, bbp_make_clickable( $at_name_at_end ) );
 118  
 119          // mentions at start of normal text, should be replaced
 120          $at_name_at_start       = sprintf( '@%s, hello', $name );
 121          $at_name_at_start_final = sprintf( '%s, hello', $link );
 122          $this->assertEquals( $at_name_at_start_final, bbp_make_clickable( $at_name_at_start ) );
 123  
 124          // mentions surrounded by square brackets
 125          $at_name_square_brackets       = sprintf( '[@%s] hello', $name );
 126          $at_name_square_brackets_final = sprintf( '[%s] hello', $link );
 127          $this->assertEquals( $at_name_square_brackets_final, bbp_make_clickable( $at_name_square_brackets ) );
 128  
 129          // mentions surrounded by round brackets
 130          $at_name_round_brackets       = sprintf( '(@%s) hello', $name );
 131          $at_name_round_brackets_final = sprintf( '(%s) hello', $link );
 132          $this->assertEquals( $at_name_round_brackets_final, bbp_make_clickable( $at_name_round_brackets ) );
 133  
 134          // mention is all text, should be replaced
 135          $at_name_is_text       = sprintf( '@%s', $name );
 136          $at_name_is_text_final = sprintf( '%s', $link );
 137          $this->assertEquals( $at_name_is_text_final, bbp_make_clickable( $at_name_is_text ) );
 138  
 139          // mention followed by colon, should be replaced
 140          $at_name_colon       = sprintf( 'Hey @%s: hello', $name );
 141          $at_name_colon_final = sprintf( 'Hey %s: hello', $link );
 142          $this->assertEquals( $at_name_colon_final, bbp_make_clickable( $at_name_colon ) );
 143  
 144          // mention followed by comma, should be replaced
 145          $at_name_comma       = sprintf( 'Hey @%s, hello', $name );
 146          $at_name_comma_final = sprintf( 'Hey %s, hello', $link );
 147          $this->assertEquals( $at_name_comma_final, bbp_make_clickable( $at_name_comma ) );
 148  
 149          // Don't link non-existent users
 150          $text = "Don't link @non @existent @users";
 151          $this->assertSame( $text, bbp_make_clickable( $text ) );
 152  
 153          remove_filter( 'bbp_make_mentions_clickable_classes', '__return_empty_array' );
 154      }
 155  
 156      /**
 157       * @group bbp_make_clickable_hits
 158       * @group bbp_make_clickable_multiple
 159       * @covers ::bbp_make_clickable
 160       */
 161  	public function test_bbp_make_clickable_multiple_mention_hits() {
 162          $u1 = $this->factory->user->create( array(
 163              'user_login'    => 'foobarbaz',
 164              'user_nicename' => 'foobarbaz',
 165          ) );
 166  
 167          $u2 = $this->factory->user->create( array(
 168              'user_login'    => 'foo2',
 169              'user_nicename' => 'foo2',
 170          ) );
 171  
 172          add_filter( 'bbp_make_mentions_clickable_classes', '__return_empty_array' );
 173  
 174          // Create the link to the user's profile
 175          $user_1   = get_userdata( $u1 );
 176          $url_1    = bbp_get_user_profile_url( $user_1->ID );
 177          $anchor_1 = '<a href="%1$s" class="">@%2$s</a>';
 178          $name_1   = $user_1->user_nicename;
 179          $link_1   = sprintf( $anchor_1, esc_url( $url_1 ), esc_html( $name_1 ) );
 180          $link_1   = bbp_rel_nofollow( $link_1 );
 181  
 182          $user_2   = get_userdata( $u2 );
 183          $url_2    = bbp_get_user_profile_url( $user_2->ID );
 184          $anchor_2 = '<a href="%1$s" class="">@%2$s</a>';
 185          $name_2   = $user_2->user_nicename;
 186          $link_2   = sprintf( $anchor_2, esc_url( $url_2 ), esc_html( $name_2 ) );
 187          $link_2   = bbp_rel_nofollow( $link_2 );
 188  
 189          // Multiples
 190          $at_name_in_mailto       = sprintf( "Send messages to @%s, @%s.", $name_1, $name_2 );
 191          $at_name_in_mailto_final = sprintf( "Send messages to %s, %s.",   $link_1, $link_2 );
 192          $this->assertEquals( $at_name_in_mailto_final, bbp_make_clickable( $at_name_in_mailto ) );
 193  
 194          // Two names separated by `&`
 195          $at_names_inbetween_ampersand       = sprintf( "Oh hi @%s & @%s.", $name_1, $name_2 );
 196          $at_names_inbetween_ampersand_final = sprintf( "Oh hi %s & %s.",   $link_1, $link_2 );
 197          $this->assertEquals( $at_names_inbetween_ampersand_final, bbp_make_clickable( $at_names_inbetween_ampersand ) );
 198  
 199          // Two names separated by `and`
 200          $at_names_inbetween_and       = sprintf( "Oh hi @%s and @%s.", $name_1, $name_2 );
 201          $at_names_inbetween_and_final = sprintf( "Oh hi %s and %s.",   $link_1, $link_2 );
 202          $this->assertEquals( $at_names_inbetween_and_final, bbp_make_clickable( $at_names_inbetween_and ) );
 203  
 204          remove_filter( 'bbp_make_mentions_clickable_classes', '__return_empty_array' );
 205      }
 206  
 207      /**
 208       * @group bbp_make_clickable_misses
 209       * @group bbp_make_clickable_multiple
 210       * @covers ::bbp_make_clickable
 211       */
 212      public function test_bbp_make_clickable_multiple_mention_misses() {
 213          $u1 = $this->factory->user->create( array(
 214              'user_login'    => 'foobarbaz',
 215              'user_nicename' => 'foobarbaz',
 216          ) );
 217  
 218          $u2 = $this->factory->user->create( array(
 219              'user_login'    => 'foo2',
 220              'user_nicename' => 'foo2',
 221          ) );
 222  
 223          add_filter( 'bbp_make_mentions_clickable_classes', '__return_empty_array' );
 224  
 225          // Create the link to the user's profile
 226          $user_1   = get_userdata( $u1 );
 227          $url_1    = bbp_get_user_profile_url( $user_1->ID );
 228          $anchor_1 = '<a href="%1$s" class="">@%2$s</a>';
 229          $name_1   = $user_1->user_nicename;
 230          $link_1   = sprintf( $anchor_1, esc_url( $url_1 ), esc_html( $name_1 ) );
 231          $link_1   = bbp_rel_nofollow( $link_1 );
 232  
 233          $user_2   = get_userdata( $u2 );
 234          $url_2    = bbp_get_user_profile_url( $user_2->ID );
 235          $anchor_2 = '<a href="%1$s" class="">@%2$s</a>';
 236          $name_2   = $user_2->user_nicename;
 237          $link_2   = sprintf( $anchor_2, esc_url( $url_2 ), esc_html( $name_2 ) );
 238          $link_2   = bbp_rel_nofollow( $link_2 );
 239  
 240          // Multiples
 241          $at_name_in_mailto       = sprintf( "Send messages to @%s, @non1, @%s, @non2.", $name_1, $name_2 );
 242          $at_name_in_mailto_final = sprintf( "Send messages to %s, @non1, %s, @non2.",   $link_1, $link_2 );
 243          $this->assertEquals( $at_name_in_mailto_final, bbp_make_clickable( $at_name_in_mailto ) );
 244  
 245          remove_filter( 'bbp_make_mentions_clickable_classes', '__return_empty_array' );
 246      }
 247  }


Generated: Tue Mar 19 01:01:02 2024 Cross-referenced by PHPXref 0.7.1