[ Index ]

PHP Cross Reference of BuddyPress

title

Body

[close]

/tests/phpunit/testcases/core/ -> class-bp-media-extractor.php (source)

   1  <?php
   2  /**
   3   * @group core
   4   * @group BP_Media_Extractor
   5   */
   6  class BP_Tests_Media_Extractor extends BP_UnitTestCase {
   7      public static $media_extractor = null;
   8      public static $richtext        = '';
   9  
  10  	public static function wpSetUpBeforeClass( $f ) {
  11          self::$media_extractor = new BP_Media_Extractor();
  12          self::$richtext        = "Hello world.
  13  
  14          This sample text is used to test the media extractor parsing class. @paulgibbs thinks it's pretty cool.
  15          Another thing really cool is this @youtube:
  16  
  17          https://www.youtube.com/watch?v=2mjvfnUAfyo
  18  
  19          This video is literally out of the world, but uses a different protocol to the embed above:
  20  
  21          http://www.youtube.com/watch?v=KaOC9danxNo
  22  
  23          <a href='https://example.com'>Testing a regular link.</a>
  24          <strong>But we should throw in some markup and maybe even an <img src='http://example.com/image.gif'>.
  25          <a href='http://example.com'><img src='http://example.com/image-in-a-link.gif' /></a></strong>.
  26          It definitely does not like <img src='data:1234567890A'>data URIs</img>. @
  27  
  28          The parser only extracts wp_allowed_protocols() protocols, not something like <a href='phone:004400'>phone</a>.
  29  
  30          [caption id='example']Here is a caption shortcode.[/caption]
  31  
  32          There are two types of [gallery] shortcodes; one like that, and another with IDs specified.
  33  
  34          Audio shortcodes:
  35          [audio src='http://example.com/source.mp3']
  36          [audio src='http://example.com/source.wav' loop='on' autoplay='off' preload='metadata'].
  37  
  38          The following shortcode should be picked up by the shortcode extractor, but not the audio extractor, because
  39          it has an unrecognised file extension (for an audio file). [audio src='http://example.com/not_audio.gif']
  40          <a href='http://example.com/more_audio.mp3'>This should be picked up, too</a>.
  41  
  42          Video shortcodes:
  43          [video src='http://example.com/source.ogv']
  44          [video src='http://example.com/source.webm' loop='on' autoplay='off' preload='metadata']
  45  
  46          The following shortcode should be picked up by the shortcode extractor, but not the video extractor, because
  47          it has an unrecognised file extension (for a video file). [video src='http://example.com/not_video.mp3']
  48          ";
  49      }
  50  
  51  
  52      /**
  53       * General.
  54       */
  55  
  56  	public function test_check_media_extraction_return_types() {
  57          $media = self::$media_extractor->extract( self::$richtext );
  58  
  59          foreach ( array( 'has', 'embeds', 'images', 'links', 'mentions', 'shortcodes', 'audio' ) as $key ) {
  60              $this->assertArrayHasKey( $key, $media );
  61              $this->assertInternalType( 'array', $media[ $key ] );
  62          }
  63  
  64          foreach ( $media['has'] as $item ) {
  65              $this->assertInternalType( 'int', $item );
  66          }
  67  
  68          foreach ( $media['links'] as $item ) {
  69              $this->assertArrayHasKey( 'url', $item );
  70              $this->assertInternalType( 'string', $item['url'] );
  71              $this->assertNotEmpty( $item['url'] );
  72          }
  73  
  74          foreach ( $media['mentions'] as $item ) {
  75              $this->assertArrayHasKey( 'name', $item );
  76              $this->assertInternalType( 'string', $item['name'] );
  77              $this->assertNotEmpty( $item['name'] );
  78          }
  79  
  80          foreach ( $media['images'] as $item ) {
  81              $this->assertArrayHasKey( 'height', $item );
  82              $this->assertInternalType( 'int', $item['height'] );
  83  
  84              $this->assertArrayHasKey( 'width', $item );
  85              $this->assertInternalType( 'int', $item['width'] );
  86  
  87              $this->assertArrayHasKey( 'source', $item );
  88              $this->assertInternalType( 'string', $item['source'] );
  89              $this->assertNotEmpty( $item['source'] );
  90  
  91              $this->assertArrayHasKey( 'url', $item );
  92              $this->assertInternalType( 'string', $item['url'] );
  93              $this->assertNotEmpty( $item['url'] );
  94          }
  95  
  96          foreach ( $media['shortcodes'] as $shortcode_type => $item ) {
  97              $this->assertArrayHasKey( 'attributes', $item );
  98              $this->assertInternalType( 'array', $item['attributes'] );
  99  
 100              $this->assertArrayHasKey( 'content', $item );
 101              $this->assertInternalType( 'string', $item['content'] );
 102  
 103              $this->assertArrayHasKey( 'type', $item );
 104              $this->assertInternalType( 'string', $item['type'] );
 105  
 106              $this->assertArrayHasKey( 'original', $item );
 107              $this->assertInternalType( 'string', $item['original'] );
 108          }
 109  
 110          foreach ( $media['embeds'] as $item ) {
 111              $this->assertArrayHasKey( 'url', $item );
 112              $this->assertInternalType( 'string', $item['url'] );
 113              $this->assertNotEmpty( $item['url'] );
 114          }
 115  
 116          foreach ( $media['audio'] as $item ) {
 117              $this->assertArrayHasKey( 'url', $item );
 118              $this->assertInternalType( 'string', $item['url'] );
 119              $this->assertNotEmpty( $item['url'] );
 120  
 121              $this->assertArrayHasKey( 'source', $item );
 122              $this->assertInternalType( 'string', $item['source'] );
 123              $this->assertNotEmpty( $item['source'] );
 124          }
 125      }
 126  
 127      public function test_check_media_extraction_counts_are_correct() {
 128          $media = self::$media_extractor->extract( self::$richtext );
 129          $types = array_keys( $media );
 130  
 131          foreach ( $types as $type ) {
 132              if ( $type === 'has' ) {
 133                  continue;
 134              }
 135  
 136              $this->assertArrayHasKey( $type, $media['has'] );
 137              $this->assertSame( count( $media[ $type ] ), $media['has'][ $type ], "Difference with the 'has' count for {$type}." );
 138          }
 139      }
 140  
 141  
 142      public function test_extract_multiple_media_types_from_content() {
 143          self::factory()->user->create( array( 'user_login' => 'paulgibbs' ) );
 144          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::LINKS | BP_Media_Extractor::MENTIONS );
 145  
 146          $this->assertNotEmpty( $media['links'] );
 147          $this->assertNotEmpty( $media['mentions'] );
 148          $this->assertArrayNotHasKey( 'shortcodes', $media );
 149      }
 150  
 151  	public function test_extract_media_from_a_wp_post() {
 152          $post_id = self::factory()->post->create( array( 'post_content' => self::$richtext ) );
 153          $media   = self::$media_extractor->extract( get_post( $post_id ), BP_Media_Extractor::LINKS );
 154  
 155          $this->assertArrayHasKey( 'links', $media );
 156          $this->assertSame( 'https://example.com', $media['links'][0]['url'] );
 157          $this->assertSame( 'http://example.com',  $media['links'][1]['url'] );
 158      }
 159  
 160  
 161      /**
 162       * Link extraction.
 163       */
 164  
 165  	public function test_extract_links_from_content() {
 166          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::LINKS );
 167  
 168          $this->assertArrayHasKey( 'links', $media );
 169          $this->assertSame( 'https://example.com', $media['links'][0]['url'] );
 170          $this->assertSame( 'http://example.com',  $media['links'][1]['url'] );
 171      }
 172  
 173      public function test_extract_no_links_from_content_with_invalid_links() {
 174          $richtext = "This is some sample text, with links, but not the kinds we want.
 175          <a href=''>Empty links should be ignore<a/> and
 176          <a href='phone:004400'>weird protocols should be ignored, too</a>.
 177          ";
 178  
 179          $media = self::$media_extractor->extract( $richtext, BP_Media_Extractor::LINKS );
 180          $this->assertSame( 0, $media['has']['links'] );
 181      }
 182  
 183  
 184      /**
 185       * at-mentions extraction.
 186       */
 187  
 188      public function test_extract_mentions_from_content_with_activity_enabled() {
 189          self::factory()->user->create( array( 'user_login' => 'paulgibbs' ) );
 190          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::MENTIONS );
 191  
 192          $this->assertArrayHasKey( 'user_id', $media['mentions'][0] );
 193          $this->assertSame( 'paulgibbs', $media['mentions'][0]['name'] );
 194      }
 195  
 196      public function test_extract_mentions_from_content_with_activity_disabled() {
 197          self::factory()->user->create( array( 'user_login' => 'paulgibbs' ) );
 198          $was_activity_enabled = false;
 199  
 200          // Turn activity off.
 201          if ( isset( buddypress()->active_components['activity'] ) ) {
 202              unset( buddypress()->active_components['activity'] );
 203              $was_activity_enabled = true;
 204          }
 205  
 206  
 207          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::MENTIONS );
 208  
 209          $this->assertArrayNotHasKey( 'user_id', $media['mentions'][0] );
 210          $this->assertSame( 'paulgibbs', $media['mentions'][0]['name'] );
 211  
 212  
 213          // Turn activity on.
 214          if ( $was_activity_enabled ) {
 215              buddypress()->active_components['activity'] = 1;
 216          }
 217      }
 218  
 219  
 220      /**
 221       * Shortcodes extraction.
 222       */
 223  
 224  	public function test_extract_shortcodes_from_content() {
 225          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::SHORTCODES );
 226  
 227          $this->assertArrayHasKey( 'shortcodes', $media );
 228  
 229          $this->assertSame( 'caption', $media['shortcodes'][0]['type'] );
 230          $this->assertSame( 'Here is a caption shortcode.', $media['shortcodes'][0]['content'] );
 231          $this->assertSame( 'example', $media['shortcodes'][0]['attributes']['id'] );
 232  
 233          $this->assertSame( 'gallery', $media['shortcodes'][1]['type'] );
 234          $this->assertEmpty( $media['shortcodes'][1]['content'] );
 235  
 236          $this->assertSame( 'audio', $media['shortcodes'][2]['type'] );
 237          $this->assertEmpty( $media['shortcodes'][2]['content'] );
 238          $this->assertSame( 'http://example.com/source.mp3', $media['shortcodes'][2]['attributes']['src'] );
 239  
 240          $this->assertSame( 'audio', $media['shortcodes'][3]['type'] );
 241          $this->assertEmpty( $media['shortcodes'][3]['content'] );
 242          $this->assertSame( 'http://example.com/source.wav', $media['shortcodes'][3]['attributes']['src'] );
 243          $this->assertSame( 'on', $media['shortcodes'][3]['attributes']['loop'] );
 244          $this->assertSame( 'off', $media['shortcodes'][3]['attributes']['autoplay'] );
 245          $this->assertSame( 'metadata', $media['shortcodes'][3]['attributes']['preload'] );
 246      }
 247  
 248      public function test_extract_no_shortcodes_from_content_with_unregistered_shortcodes() {
 249          $richtext = 'This sample text has some made-up [fake]shortcodes[/fake].';
 250  
 251          $media = self::$media_extractor->extract( $richtext, BP_Media_Extractor::SHORTCODES );
 252          $this->assertSame( 0, $media['has']['shortcodes'] );
 253      }
 254  
 255  
 256      /**
 257       * oEmbeds extraction.
 258       */
 259  
 260  	public function test_extract_oembeds_from_content() {
 261          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::EMBEDS );
 262  
 263          $this->assertArrayHasKey( 'embeds', $media );
 264          $this->assertSame( 'https://www.youtube.com/watch?v=2mjvfnUAfyo', $media['embeds'][0]['url'] );
 265          $this->assertSame( 'http://www.youtube.com/watch?v=KaOC9danxNo',  $media['embeds'][1]['url'] );
 266      }
 267  
 268  
 269      /**
 270       * Images extraction (src tags).
 271       */
 272  
 273      // both quote styles
 274      public function test_extract_images_from_content_with_src_tags() {
 275          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::IMAGES );
 276  
 277          $this->assertArrayHasKey( 'images', $media );
 278          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'html' ) ) );
 279  
 280          $this->assertSame( 'http://example.com/image.gif',           $media[0]['url'] );
 281          $this->assertSame( 'http://example.com/image-in-a-link.gif', $media[1]['url'] );
 282      }
 283  
 284      // empty src attributes, data: URIs
 285      public function test_extract_no_images_from_content_with_invalid_src_tags() {
 286          $richtext = 'This sample text will contain images with invalid src tags, like this:
 287          <img src="data://abcd"> or <img src="phone://0123" />.
 288          ';
 289  
 290          $media = self::$media_extractor->extract( $richtext, BP_Media_Extractor::IMAGES );
 291  
 292          $this->assertArrayHasKey( 'images', $media );
 293          $this->assertSame( 0, $media['has']['images'] );
 294      }
 295  
 296  
 297      /**
 298       * Images extraction (galleries).
 299       */
 300  
 301      public function test_extract_images_from_content_with_galleries_variant_no_ids() {
 302          // To test the [gallery] shortcode, we need to create a post and an attachment.
 303          $post_id       = self::factory()->post->create( array( 'post_content' => self::$richtext ) );
 304          $attachment_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
 305              'post_mime_type' => 'image/jpeg',
 306              'post_type'      => 'attachment'
 307          ) );
 308          wp_update_attachment_metadata( $attachment_id, array( 'width' => 100, 'height' => 100 ) );
 309  
 310  
 311          // Extract the gallery images.
 312          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::IMAGES, array(
 313              'post' => get_post( $post_id ),
 314          ) );
 315  
 316          $this->assertArrayHasKey( 'images', $media );
 317          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'galleries' ) ) );
 318          $this->assertCount( 1, $media );
 319  
 320          $this->assertSame( WP_CONTENT_URL . '/uploads/image.jpg', $media[0]['url'] );
 321      }
 322  
 323      public function test_extract_images_from_content_with_galleries_variant_ids() {
 324          $attachment_ids = array(
 325              'large'  => 0,
 326              'canola' => 0,
 327          );
 328  
 329          $attachment_ids['large'] = self::factory()->attachment->create_upload_object( BP_TESTS_DIR . 'assets/test-image-large.jpg' );
 330          $attachment_ids['canola'] = self::factory()->attachment->create_upload_object( BP_TESTS_DIR . 'assets/canola.jpg' );
 331  
 332          $attachments = join( ',', $attachment_ids );
 333          $post_id     = self::factory()->post->create( array( 'post_content' => "[gallery ids='{$attachments}']" ) );
 334  
 335          // Extract the gallery images.
 336          $media = self::$media_extractor->extract( '', BP_Media_Extractor::IMAGES, array(
 337              'post' => get_post( $post_id ),
 338          ) );
 339  
 340          $this->assertArrayHasKey( 'images', $media );
 341          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'galleries' ) ) );
 342          $this->assertCount( 2, $media );
 343  
 344          $expected_urls = array(
 345              'large'  => wp_get_attachment_url( $attachment_ids['large'] ),
 346              'canola' => wp_get_attachment_url( $attachment_ids['canola'] ),
 347          );
 348  
 349          $this->assertSame( $expected_urls['large'], $media[0]['url'] );
 350          $this->assertSame( $expected_urls['canola'], $media[1]['url'] );
 351      }
 352  
 353      public function test_extract_no_images_from_content_with_invalid_galleries_variant_no_ids() {
 354          $post_id = self::factory()->post->create( array( 'post_content' => self::$richtext ) );
 355          $media   = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::IMAGES, array(
 356              'post' => get_post( $post_id ),
 357          ) );
 358  
 359          $this->assertArrayHasKey( 'images', $media );
 360          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'galleries' ) ) );
 361          $this->assertCount( 0, $media );
 362      }
 363  
 364      public function test_extract_no_images_from_content_with_invalid_galleries_variant_ids() {
 365          $post_id = self::factory()->post->create( array( 'post_content' => '[gallery ids="117,4529"]' ) );
 366          $media   = self::$media_extractor->extract( '', BP_Media_Extractor::IMAGES, array(
 367              'post' => get_post( $post_id ),
 368          ) );
 369  
 370          $this->assertArrayHasKey( 'images', $media );
 371          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'galleries' ) ) );
 372          $this->assertCount( 0, $media );
 373      }
 374  
 375  
 376      /**
 377       * Images extraction (thumbnail).
 378       */
 379  
 380      public function test_extract_no_images_from_content_with_featured_image() {
 381          $post_id      = self::factory()->post->create( array( 'post_content' => self::$richtext ) );
 382          $thumbnail_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
 383              'post_mime_type' => 'image/jpeg',
 384              'post_type'      => 'attachment'
 385          ) );
 386          set_post_thumbnail( $post_id, $thumbnail_id );
 387  
 388  
 389          // Extract the gallery images.
 390          $media = self::$media_extractor->extract( '', BP_Media_Extractor::IMAGES, array(
 391              'post' => get_post( $post_id ),
 392          ) );
 393  
 394          $this->assertArrayHasKey( 'images', $media );
 395          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'featured_images' ) ) );
 396          $this->assertCount( 1, $media );
 397  
 398          $this->assertSame( WP_CONTENT_URL . '/uploads/image.jpg', $media[0]['url'] );
 399      }
 400  
 401      public function test_extract_images_from_content_without_featured_image() {
 402          $post_id = self::factory()->post->create( array( 'post_content' => self::$richtext ) );
 403          $media   = self::$media_extractor->extract( '', BP_Media_Extractor::IMAGES, array(
 404              'post' => get_post( $post_id ),
 405          ) );
 406  
 407          $this->assertArrayHasKey( 'images', $media );
 408          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'featured_images' ) ) );
 409          $this->assertCount( 0, $media );
 410      }
 411  
 412  
 413      /**
 414       * Audio extraction.
 415       */
 416  
 417  	public function test_extract_audio_from_content() {
 418          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::AUDIO );
 419  
 420          $this->assertArrayHasKey( 'audio', $media );
 421          $this->assertCount( 3, $media['audio'] );
 422  
 423          $this->assertSame( 'shortcodes', $media['audio'][0]['source'] );
 424          $this->assertSame( 'shortcodes', $media['audio'][1]['source'] );
 425          $this->assertSame( 'html',       $media['audio'][2]['source'] );
 426  
 427          $this->assertSame( 'http://example.com/source.mp3',     $media['audio'][0]['url'] );
 428          $this->assertSame( 'http://example.com/source.wav',     $media['audio'][1]['url'] );
 429          $this->assertSame( 'http://example.com/more_audio.mp3', $media['audio'][2]['url'] );
 430      }
 431  
 432      public function test_extract_audio_shortcode_with_no_src_param() {
 433          $richtext = '[audio http://example.com/a-song.mp3]';
 434          $media = self::$media_extractor->extract( $richtext, BP_Media_Extractor::AUDIO );
 435  
 436          $this->assertArrayHasKey( 'audio', $media );
 437          $this->assertCount( 1, $media['audio'] );
 438          $this->assertSame( 'http://example.com/a-song.mp3', $media['audio'][0]['url'] );
 439      }
 440  
 441  	public function test_extract_no_audio_from_invalid_content() {
 442          $richtext = '[audio src="http://example.com/not_audio.gif"]
 443          <a href="http://example.com/more_not_audio.mp33">Hello</a>.';
 444  
 445          $media = self::$media_extractor->extract( $richtext, BP_Media_Extractor::AUDIO );
 446          $this->assertSame( 0, $media['has']['audio'] );
 447      }
 448  
 449      public function test_extract_no_audio_from_empty_audio_shortcode() {
 450          $media = self::$media_extractor->extract( '[audio]', BP_Media_Extractor::AUDIO );
 451          $this->assertSame( 0, $media['has']['audio'] );
 452      }
 453  
 454  
 455      /**
 456       * Video extraction.
 457       */
 458  
 459  	public function test_extract_video_from_content() {
 460          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::VIDEOS );
 461  
 462          $this->assertArrayHasKey( 'videos', $media );
 463          $this->assertCount( 2, $media['videos'] );
 464  
 465          $this->assertSame( 'shortcodes', $media['videos'][0]['source'] );
 466          $this->assertSame( 'shortcodes', $media['videos'][1]['source'] );
 467  
 468          $this->assertSame( 'http://example.com/source.ogv',  $media['videos'][0]['url'] );
 469          $this->assertSame( 'http://example.com/source.webm', $media['videos'][1]['url'] );
 470      }
 471  
 472  
 473      public function test_extract_video_shortcode_with_no_src_param() {
 474          $richtext = '[video http://example.com/source.ogv]';
 475          $media = self::$media_extractor->extract( $richtext, BP_Media_Extractor::VIDEOS );
 476  
 477          $this->assertArrayHasKey( 'videos', $media );
 478          $this->assertCount( 1, $media['videos'] );
 479          $this->assertSame( 'http://example.com/source.ogv', $media['videos'][0]['url'] );
 480      }
 481  
 482  	public function test_extract_no_video_from_invalid_content() {
 483          $richtext = '[video src="http://example.com/not_video.mp3"]';
 484          $media    = self::$media_extractor->extract( $richtext, BP_Media_Extractor::VIDEOS );
 485  
 486          $this->assertSame( 0, $media['has']['videos'] );
 487      }
 488  
 489      public function test_extract_no_videos_from_empty_video_shortcodes() {
 490          $media = self::$media_extractor->extract( '[video]', BP_Media_Extractor::VIDEOS );
 491          $this->assertSame( 0, $media['has']['videos'] );
 492      }
 493  }


Generated: Sun Dec 22 01:00:54 2024 Cross-referenced by PHPXref 0.7.1