You are here

public function ViewsContentCacheUnitTest::testQueryBuilder in Views content cache 7.3

Same name and namespace in other branches
  1. 6.2 tests/views_content_cache.test \ViewsContentCacheUnitTest::testQueryBuilder()

File

tests/views_content_cache.test, line 202
Provides the Views content cache tests.

Class

ViewsContentCacheUnitTest

Code

public function testQueryBuilder() {
  $query_arrays = array(
    // First test
    array(
      '#glue' => 'AND',
      array(
        '#clause' => 'FIELDA = %d',
        '#args' => array(
          1,
        ),
      ),
      array(
        '#clause' => 'FIELDB = %d',
        '#args' => array(
          2,
        ),
      ),
      array(
        '#clause' => 'FIELDC = %d',
        '#args' => array(
          3,
        ),
      ),
    ),
    //Second test:
    array(
      '#glue' => 'OR',
      array(
        '#clause' => 'FIELDA = %d',
        '#args' => array(
          3,
        ),
      ),
      array(
        '#clause' => 'FIELDB = %d',
        '#args' => array(
          2,
        ),
      ),
      array(
        '#clause' => 'FIELDC = %d',
        '#args' => array(
          1,
        ),
      ),
    ),
    // Third test:
    array(
      '#glue' => 'OR',
      array(
        '#glue' => 'AND',
        array(
          '#clause' => 'FIELDA = %d',
          '#args' => array(
            1,
            2,
          ),
        ),
        array(
          '#clause' => 'FIELDB = %d',
          '#args' => array(
            3,
            4,
          ),
        ),
      ),
      array(
        '#clause' => 'FIELDC = %d',
        '#args' => array(
          5,
          6,
        ),
      ),
    ),
    // Fourth test:
    array(
      '#glue' => 'OR',
      array(
        '#clause' => 'FIELDA = %d',
        '#args' => array(
          5,
          6,
        ),
      ),
      array(
        '#glue' => 'AND',
        array(
          '#clause' => 'FIELDB = %d',
          '#args' => array(
            1,
            2,
          ),
        ),
        array(
          '#clause' => 'FIELDC = %d',
          '#args' => array(
            3,
            4,
          ),
        ),
      ),
    ),
    // Fifth test:
    array(
      '#glue' => 'OR',
      array(
        '#clause' => 'FIELDA = %d',
        '#args' => array(
          5,
          6,
        ),
      ),
      array(
        '#glue' => 'AND',
        array(
          '#clause' => 'FIELDB = %d',
          '#args' => array(
            1,
            2,
          ),
        ),
        array(
          '#glue' => 'OR',
          array(
            '#clause' => 'FIELDC = %d',
            '#args' => array(
              3,
              4,
            ),
          ),
          array(
            '#clause' => 'FIELDD = %d',
            '#args' => array(
              7,
              8,
            ),
          ),
        ),
        array(
          '#clause' => 'FIELDE = %d',
          '#args' => array(
            9,
            10,
          ),
        ),
      ),
    ),
    // Sixth test:
    array(
      '#glue' => 'OR',
      array(
        '#glue' => 'AND',
      ),
      array(
        '#clause' => 'FIELDA = %d',
        '#args' => array(
          5,
          6,
        ),
      ),
    ),
  );
  $expected_where = array(
    '(FIELDA = %d AND FIELDB = %d AND FIELDC = %d)',
    '(FIELDA = %d OR FIELDB = %d OR FIELDC = %d)',
    '((FIELDA = %d AND FIELDB = %d) OR FIELDC = %d)',
    '(FIELDA = %d OR (FIELDB = %d AND FIELDC = %d))',
    '(FIELDA = %d OR (FIELDB = %d AND (FIELDC = %d OR FIELDD = %d) AND FIELDE = %d))',
    '(FIELDA = %d)',
  );
  $expected_args = array(
    array(
      1,
      2,
      3,
    ),
    array(
      3,
      2,
      1,
    ),
    array(
      1,
      2,
      3,
      4,
      5,
      6,
    ),
    array(
      5,
      6,
      1,
      2,
      3,
      4,
    ),
    array(
      5,
      6,
      1,
      2,
      3,
      4,
      7,
      8,
      9,
      10,
    ),
    array(
      5,
      6,
    ),
  );
  foreach ($query_arrays as $key => $query) {
    $built = views_content_cache_query_builder($query);
    $this
      ->assertEqual($built['#clause'], $expected_where[$key], t('Built the query: "@query", expected: "@expected".', array(
      '@query' => $built['#clause'],
      '@expected' => $expected_where[$key],
    )));
    $this
      ->assertIdentical($built['#args'], $expected_args[$key], t('Built the query arguments.'));
  }
}