| 1952 | |
| 1953 | /** |
| 1954 | * @ticket BP8340 |
| 1955 | */ |
| 1956 | public function test_oembed() { |
| 1957 | $u1 = self::factory()->user->create(); |
| 1958 | $a1 = self::factory()->activity->create( |
| 1959 | array( |
| 1960 | 'user_id' => $u1, |
| 1961 | 'component' => 'activity', |
| 1962 | 'type' => 'activity_update', |
| 1963 | 'content' => 'Test |
| 1964 | |
| 1965 | https://www.youtube.com/watch?v=8Are9dDbW24 |
| 1966 | |
| 1967 | Ha', |
| 1968 | ) |
| 1969 | ); |
| 1970 | |
| 1971 | // Helper to instantiate activity loop and check activity for oEmbed. |
| 1972 | $loop = function( $id ) { |
| 1973 | bp_has_activities( [ 'in' => $id ] ); |
| 1974 | bp_the_activity(); |
| 1975 | |
| 1976 | // Assert that YouTube link was converted to iframe. |
| 1977 | add_filter( 'oembed_result', array( $this, 'check_oembed_ping' ) ); |
| 1978 | $this->assertContains( '<iframe', bp_get_activity_content_body() ); |
| 1979 | |
| 1980 | // Uncomment to check loading="lazy" attribute addition |
| 1981 | //$this->assertContains( '<iframe loading="lazy"', bp_get_activity_content_body() ); |
| 1982 | |
| 1983 | remove_filter( 'oembed_result', array( $this, 'check_oembed_ping' ) ); |
| 1984 | }; |
| 1985 | |
| 1986 | // Instantiate activity loop for the first time. |
| 1987 | $loop( $a1 ); |
| 1988 | |
| 1989 | // Check that oEmbed was used. |
| 1990 | $this->assertTrue( self::$ping ); |
| 1991 | |
| 1992 | // Now, check that oEmbed was cached in activity meta. |
| 1993 | $meta = bp_activity_get_meta( $a1 ); |
| 1994 | $cache = current( current( $meta ) ); |
| 1995 | $this->assertContains( '<iframe', $cache ); |
| 1996 | $this->assertTrue( 0 === strpos( key( $meta ), '_oembed_' ) ); |
| 1997 | |
| 1998 | // Lastly, instantiate activity loop for a second time. |
| 1999 | self::$ping = false; |
| 2000 | $loop( $a1 ); |
| 2001 | |
| 2002 | // oEmbed shouldn't have fired this time around. |
| 2003 | $this->assertFalse( self::$ping ); |
| 2004 | |
| 2005 | // Clean up after ourselves! |
| 2006 | $GLOBALS['activities_template'] = null; |
| 2007 | } |
| 2008 | |
| 2009 | /** |
| 2010 | * Helper method to determine if we used oEmbed to ping a provider. |
| 2011 | */ |
| 2012 | public function check_oembed_ping( $retval ) { |
| 2013 | self::$ping = true; |
| 2014 | return $retval; |
| 2015 | } |