[ 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          $attachment_ids[] = $this->fake_attachment_upload( DIR_TESTDATA . '/images/test-image-large.png' );
 326          $attachment_ids[] = $this->fake_attachment_upload( DIR_TESTDATA . '/images/canola.jpg' );
 327          $attachment_ids   = join( ',', $attachment_ids );
 328          $post_id          = self::factory()->post->create( array( 'post_content' => "[gallery ids='{$attachment_ids}']" ) );
 329  
 330          // Extract the gallery images.
 331          $media = self::$media_extractor->extract( '', BP_Media_Extractor::IMAGES, array(
 332              'post' => get_post( $post_id ),
 333          ) );
 334  
 335          $this->assertArrayHasKey( 'images', $media );
 336          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'galleries' ) ) );
 337          $this->assertCount( 2, $media );
 338  
 339          $this->assertSame( WP_CONTENT_URL . '/uploads/test-image-large.png', $media[0]['url'] );
 340          $this->assertSame( WP_CONTENT_URL . '/uploads/canola.jpg', $media[1]['url'] );
 341      }
 342  
 343      public function test_extract_no_images_from_content_with_invalid_galleries_variant_no_ids() {
 344          $post_id = self::factory()->post->create( array( 'post_content' => self::$richtext ) );
 345          $media   = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::IMAGES, array(
 346              'post' => get_post( $post_id ),
 347          ) );
 348  
 349          $this->assertArrayHasKey( 'images', $media );
 350          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'galleries' ) ) );
 351          $this->assertCount( 0, $media );
 352      }
 353  
 354      public function test_extract_no_images_from_content_with_invalid_galleries_variant_ids() {
 355          $post_id = self::factory()->post->create( array( 'post_content' => '[gallery ids="117,4529"]' ) );
 356          $media   = self::$media_extractor->extract( '', BP_Media_Extractor::IMAGES, array(
 357              'post' => get_post( $post_id ),
 358          ) );
 359  
 360          $this->assertArrayHasKey( 'images', $media );
 361          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'galleries' ) ) );
 362          $this->assertCount( 0, $media );
 363      }
 364  
 365  
 366      /**
 367       * Images extraction (thumbnail).
 368       */
 369  
 370      public function test_extract_no_images_from_content_with_featured_image() {
 371          $post_id      = self::factory()->post->create( array( 'post_content' => self::$richtext ) );
 372          $thumbnail_id = self::factory()->attachment->create_object( 'image.jpg', $post_id, array(
 373              'post_mime_type' => 'image/jpeg',
 374              'post_type'      => 'attachment'
 375          ) );
 376          set_post_thumbnail( $post_id, $thumbnail_id );
 377  
 378  
 379          // Extract the gallery images.
 380          $media = self::$media_extractor->extract( '', BP_Media_Extractor::IMAGES, array(
 381              'post' => get_post( $post_id ),
 382          ) );
 383  
 384          $this->assertArrayHasKey( 'images', $media );
 385          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'featured_images' ) ) );
 386          $this->assertCount( 1, $media );
 387  
 388          $this->assertSame( WP_CONTENT_URL . '/uploads/image.jpg', $media[0]['url'] );
 389      }
 390  
 391      public function test_extract_images_from_content_without_featured_image() {
 392          $post_id = self::factory()->post->create( array( 'post_content' => self::$richtext ) );
 393          $media   = self::$media_extractor->extract( '', BP_Media_Extractor::IMAGES, array(
 394              'post' => get_post( $post_id ),
 395          ) );
 396  
 397          $this->assertArrayHasKey( 'images', $media );
 398          $media = array_values( wp_list_filter( $media['images'], array( 'source' => 'featured_images' ) ) );
 399          $this->assertCount( 0, $media );
 400      }
 401  
 402  
 403      /**
 404       * Audio extraction.
 405       */
 406  
 407  	public function test_extract_audio_from_content() {
 408          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::AUDIO );
 409  
 410          $this->assertArrayHasKey( 'audio', $media );
 411          $this->assertCount( 3, $media['audio'] );
 412  
 413          $this->assertSame( 'shortcodes', $media['audio'][0]['source'] );
 414          $this->assertSame( 'shortcodes', $media['audio'][1]['source'] );
 415          $this->assertSame( 'html',       $media['audio'][2]['source'] );
 416  
 417          $this->assertSame( 'http://example.com/source.mp3',     $media['audio'][0]['url'] );
 418          $this->assertSame( 'http://example.com/source.wav',     $media['audio'][1]['url'] );
 419          $this->assertSame( 'http://example.com/more_audio.mp3', $media['audio'][2]['url'] );
 420      }
 421  
 422      public function test_extract_audio_shortcode_with_no_src_param() {
 423          $richtext = '[audio http://example.com/a-song.mp3]';
 424          $media = self::$media_extractor->extract( $richtext, BP_Media_Extractor::AUDIO );
 425  
 426          $this->assertArrayHasKey( 'audio', $media );
 427          $this->assertCount( 1, $media['audio'] );
 428          $this->assertSame( 'http://example.com/a-song.mp3', $media['audio'][0]['url'] );
 429      }
 430  
 431  	public function test_extract_no_audio_from_invalid_content() {
 432          $richtext = '[audio src="http://example.com/not_audio.gif"]
 433          <a href="http://example.com/more_not_audio.mp33">Hello</a>.';
 434  
 435          $media = self::$media_extractor->extract( $richtext, BP_Media_Extractor::AUDIO );
 436          $this->assertSame( 0, $media['has']['audio'] );
 437      }
 438  
 439      public function test_extract_no_audio_from_empty_audio_shortcode() {
 440          $media = self::$media_extractor->extract( '[audio]', BP_Media_Extractor::AUDIO );
 441          $this->assertSame( 0, $media['has']['audio'] );
 442      }
 443  
 444  
 445      /**
 446       * Video extraction.
 447       */
 448  
 449  	public function test_extract_video_from_content() {
 450          $media = self::$media_extractor->extract( self::$richtext, BP_Media_Extractor::VIDEOS );
 451  
 452          $this->assertArrayHasKey( 'videos', $media );
 453          $this->assertCount( 2, $media['videos'] );
 454  
 455          $this->assertSame( 'shortcodes', $media['videos'][0]['source'] );
 456          $this->assertSame( 'shortcodes', $media['videos'][1]['source'] );
 457  
 458          $this->assertSame( 'http://example.com/source.ogv',  $media['videos'][0]['url'] );
 459          $this->assertSame( 'http://example.com/source.webm', $media['videos'][1]['url'] );
 460      }
 461  
 462  
 463      public function test_extract_video_shortcode_with_no_src_param() {
 464          $richtext = '[video http://example.com/source.ogv]';
 465          $media = self::$media_extractor->extract( $richtext, BP_Media_Extractor::VIDEOS );
 466  
 467          $this->assertArrayHasKey( 'videos', $media );
 468          $this->assertCount( 1, $media['videos'] );
 469          $this->assertSame( 'http://example.com/source.ogv', $media['videos'][0]['url'] );
 470      }
 471  
 472  	public function test_extract_no_video_from_invalid_content() {
 473          $richtext = '[video src="http://example.com/not_video.mp3"]';
 474          $media    = self::$media_extractor->extract( $richtext, BP_Media_Extractor::VIDEOS );
 475  
 476          $this->assertSame( 0, $media['has']['videos'] );
 477      }
 478  
 479      public function test_extract_no_videos_from_empty_video_shortcodes() {
 480          $media = self::$media_extractor->extract( '[video]', BP_Media_Extractor::VIDEOS );
 481          $this->assertSame( 0, $media['has']['videos'] );
 482      }
 483  }


Generated: Mon Oct 21 01:01:35 2019 Cross-referenced by PHPXref 0.7.1