HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux wordpress-ubuntu-s-2vcpu-4gb-fra1-01 5.4.0-169-generic #187-Ubuntu SMP Thu Nov 23 14:52:28 UTC 2023 x86_64
User: root (0)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/delta/wp-content/themes/delta/vendor/timber/timber/tests/test-timber-iterator.php
<?php

class TestTimberIterator extends Timber_UnitTestCase {

    function testQueryPosts(){
        self::factory()->post->create();
        $posts = TimberPostGetter::query_posts('post_type=post');
        $this->assertInstanceOf( 'TimberQueryIterator', $posts );
    }

    function testTheLoop(){
        for ( $i = 1; $i < 3; $i++ ) {
            self::factory()->post->create( array(
                'post_title' => 'TestPost' . $i,
                'post_date' => ('2018-09-0'.$i.' 01:56:01')
            ) );
        }
        $results = Timber::compile('assets/iterator-test.twig', array(
            'posts' => TimberPostGetter::query_posts( 'post_type=post' )
        ) );

        $results = trim( $results );
        $this->assertStringStartsWith( 'TestPost2', $results );
        $this->assertStringEndsWith( 'TestPost1', $results );

    }

    function testTwigLoopVar() {
	    $posts = self::factory()->post->create_many( 3 );
	    $posts = TimberPostGetter::query_posts($posts);

	    $compiled = Timber::compile('assets/iterator-loop-test.twig', array(
		    'posts' => TimberPostGetter::query_posts( 'post_type=post' )
	    ) );

	    $loop = array_map('json_decode', explode("\n", trim($compiled)));

	    $this->assertSame(1, $loop[0]->index);
	    $this->assertSame(2, $loop[0]->revindex0);
	    $this->assertSame(3, $loop[0]->length);
	    $this->assertTrue($loop[0]->first);
	    $this->assertFalse($loop[0]->last);

	    $this->assertSame(2, $loop[1]->index);
	    $this->assertSame(1, $loop[1]->revindex0);
	    $this->assertSame(3, $loop[1]->length);
	    $this->assertFalse($loop[1]->first);
	    $this->assertFalse($loop[1]->last);

	    $this->assertSame(3, $loop[2]->index);
	    $this->assertSame(0, $loop[2]->revindex0);
	    $this->assertSame(3, $loop[2]->length);
	    $this->assertFalse($loop[2]->first);
	    $this->assertTrue($loop[2]->last);
    }

    function testPostCount() {
    	$posts = self::factory()->post->create_many( 8 );
        $posts = TimberPostGetter::query_posts('post_type=post');
        $this->assertEquals( 8, $posts->post_count() );
        $this->assertEquals( 8, count($posts) );
    }

}