You are here

public function FppServicesTest::testFppIndex in Fieldable Panels Panes (FPP) 7

Tests FPP resource Index.

File

tests/fpp.services.test, line 114
Tests the services resource Fieldable Panels Pane methods and actions.

Class

FppServicesTest
Test the services resource Fieldable Panels Pane methods and actions.

Code

public function testFppIndex() {
  $privileged_user = $this
    ->drupalCreateUser(array(
    'administer services',
    'administer fieldable panels panes',
    'perform unlimited index queries',
  ));
  $this
    ->drupalLogin($privileged_user);

  // Create a set of FPP entities. The FPP resource returns 20 items at a
  // time, so this creates 2 1/2 pages worth.
  $fpps = array();
  $count = 50;
  for ($i = 0; $i < $count; $i++) {
    $fpp = $this
      ->createFpp();
    $fpps[$fpp->fpid] = $fpp;
  }

  // Get the content.
  $page_count = ceil(count($fpps) / 20);
  $retrieved_fpps = array();
  for ($page = 0; $page < $page_count; $page++) {
    $responseArray = $this
      ->servicesGet($this->endpoint->path . '/fieldable_panels_panes', array(
      'page' => $page,
      'fields' => 'fpid,title',
    ));
    $this
      ->assertTrue(count($responseArray['body']) <= 20, 'Correct number of items returned');

    // Store the returned FPP IDs.
    foreach ($responseArray['body'] as $fpp) {
      if (isset($retrieved_fpps[$fpp->fpid])) {
        $this
          ->fail(format_string('Duplicate fieldable panels pane @fpid returned.', array(
          '@fpid' => $fpp->fpid,
        )));
      }
      $retrieved_fpps[$fpp->fpid] = TRUE;
      $this
        ->assertEqual($fpps[$fpp->fpid]->title, $fpp->title, 'Successfully received fieldable panels pane info', 'fppResource: Index');
    }
  }

  // Verify we retrieved all the FPPs.
  $expected_fpids = array_keys($fpps);
  sort($expected_fpids);
  $retrieved_fpids = array_keys($retrieved_fpps);
  sort($retrieved_fpids);
  $this
    ->assertEqual($expected_fpids, $retrieved_fpids, 'Retrieved all fieldable panels panes');

  // The n+1 page should be empty.
  $responseArray = $this
    ->servicesGet($this->endpoint->path . '/fieldable_panels_panes', array(
    'page' => $page_count + 1,
  ));
  $this
    ->assertEqual(count($responseArray['body']), 0, 'The n+1 page is empty');

  // Adjust the pager size.
  $responseArray = $this
    ->servicesGet($this->endpoint->path . '/fieldable_panels_panes', array(
    'fields' => 'fpid,title',
    'pagesize' => 40,
  ));
  $this
    ->assertEqual(count($responseArray['body']), 40, 'Correct number of items returned');

  // Swap to user that can only use the default pager size.
  $less_privileged_user = $this
    ->drupalCreateUser(array(
    'administer services',
    'administer fieldable panels panes',
  ));
  $this
    ->drupalLogin($less_privileged_user);
  $responseArray = $this
    ->servicesGet($this->endpoint->path . '/fieldable_panels_panes', array(
    'fields' => 'fpid,title',
    'pagesize' => 40,
  ));
  $this
    ->assertEqual(count($responseArray['body']), 20, 'Correct number of items returned');
}