You are here

class ReportTest in Forena Reports 8

@group Forena @require module forena @coversDefaultClass \Drupal\forena\Report

Hierarchy

Expanded class hierarchy of ReportTest

File

tests/src/Unit/ReportTest.php, line 16
Implements ReportTest

Namespace

Drupal\Tests\forena\Unit
View source
class ReportTest extends FrxTestCase {

  // Test report.
  private $doc = '<?xml version="1.0"?>
      <!DOCTYPE root [
      <!ENTITY nbsp "&#160;">
      ]>
      <html xmlns:frx="urn:FrxReports">
      <head>
      <title>Report Title</title>
      <frx:category>Category</frx:category>
      <frx:options></frx:options>
      <frx:fields>
        <frx:field id="test_field" link="link" class="class">Default Value</frx:field>
      </frx:fields>
      <frx:commands>
        <frx:ajax command="invoke" method="attr" selector="input#myinput">
          ["checked", "1"]
        </frx:ajax>
      </frx:commands>
      </head>
      <body>
        Report Body
        <div>
          <frx:ajax command="invoke" />
        </div>
        This &amp; That
      </body>
      </html>';
  private $r;

  /**
   * Ensure that report object can be created.
   */
  public function testReportParse() {

    // Sample Report To parse.
    $r = new Report($this->doc);

    // Title Check
    $this
      ->assertObjectHasAttribute('title', $r);
    $this
      ->assertEquals("Report Title", $r->title, "Correct Title");

    // Category Check.
    $this
      ->assertObjectHasAttribute('category', $r);
    $this
      ->assertEquals("Category", $r->category);

    // Check to make sure fields are parsed.
    $this
      ->assertArrayHasKey('test_field', $r->replacer->fields);
    $this
      ->assertEquals(1, count($r->commands), 'Commands parsed');
  }

  /**
   * Simple Render Test
   */
  public function testSimpleRender() {
    $this
      ->documentManager()
      ->setDocument('drupal');
    $this
      ->documentManager()
      ->getDocument()
      ->clear();
    $r = new Report($this->doc);
    $r
      ->render('drupal', FALSE);
    $content = $this
      ->getDocument()
      ->flush();

    // Make sure we have a report.
    $this
      ->assertArrayHasKey('report', $content);
    $this
      ->assertContains('Report Body', $content['report']['#template']);

    // Make sure we don't have a parameters form.
    $this
      ->assertArrayNotHasKey('parameters', $content);

    // Make sure that we don't have the the ajax command
    $this
      ->assertNotContains('ajax', $content['report']['#template']);

    //echo  $content['report']['#template'];
    $this
      ->assertContains('This &amp; That', $content['report']['#template']);
  }

  /**
   * Test a simple report.
   */
  public function testSimpleReport() {
    $this
      ->initParametersForm();
    $content = $this
      ->report('sample', [
      'specified_parameter' => 'specified',
    ]);
    $this
      ->assertArrayHasKey('report', $content);
    $this
      ->assertContains('<table>', $content['report']['#template']);

    // Make sure we have a title
    $this
      ->assertArrayHasKey('#title', $content);
    $this
      ->assertEquals('Sample Report', $content['#title'], 'Content has title');

    // Check to make sure our default parameter was set
    $parms = $this
      ->getDataContext('parm');
    $this
      ->assertArrayHasKey('default_parameter', $parms);
    $this
      ->assertEquals('test', $parms['default_parameter']);

    // Make sure passed parameters survive.
    $this
      ->assertArrayHasKey('specified_parameter', $parms);
    $this
      ->assertEquals('specified', $parms['specified_parameter']);

    // Check to make sure we have content.
    $this
      ->assertArrayHasKey('report', $content);
    $this
      ->assertArrayHasKey('#template', $content['report']);
    $this
      ->assertContains('<table>', $content['report']['#template']);
    $this
      ->assertContains('this &amp; that', $content['report']['#template']);

    // Verify that a parameters form has been build
    $this
      ->assertArrayHasKey('parameters', $content);
    $form = $content['parameters'];
    $this
      ->assertArrayHasKey('parms', $form);
    $this
      ->assertArrayHasKey('default_parameter', $form['parms']);

    // Verify that the css js library has been loaded
    $this
      ->assertArrayHasKey('#attached', $content);
    $this
      ->assertArrayHasKey('library', $content['#attached']);
    $library = $content['#attached']['library'];
    $this
      ->assertContains('forena/skin.default', $library, "Skin Library Loaded");
    $this
      ->assertContains('core/drupal.ajax', $library, "Core library added");
    $this
      ->assertContains('core/drupal.dialog.ajax', $library, "Report Specific Library added");
  }
  public function testParameterTypes() {
    $content = $this
      ->report('parameter_test');

    // Verfiy that a parameters form has been build
    $this
      ->assertArrayHasKey('parameters', $content);
    $form = $content['parameters'];
    $this
      ->assertArrayHasKey('parms', $form);

    //@TODO: Extend testing to cover all parameter control types.
    $parms = $form['parms'];
    $this
      ->assertArrayHasKey('textfield', $parms);
    $this
      ->assertArrayHasKey('select', $parms);
  }
  public function testIncludedReport() {
    $content = $this
      ->report('include');
    $report = $content['report']['#template'];
    $this
      ->assertContains('Header', $report, 'Header in tact');
    $this
      ->assertContains('col1', $report, 'Embedded report.');
    $this
      ->assertContains('Footer', $report, 'Footer in tact');
    $this
      ->assertContains('<a href', $report, 'Link got generated');
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FrxAPI::app public function Returns containing application service
FrxAPI::currentDataContext public function Get the current data context.
FrxAPI::currentDataContextArray public function
FrxAPI::dataManager public function Returns the data manager service
FrxAPI::dataService public function Return Data Service
FrxAPI::documentManager public function Returns the fornea document manager
FrxAPI::error public function Report an error
FrxAPI::getDataContext public function Get the context of a specific id.
FrxAPI::getDocument public function Get the current document
FrxAPI::getReportFileContents public function Load the contents of a file in the report file system.
FrxAPI::innerXML function Enter description here... 1
FrxAPI::popData public function Pop data off of the stack.
FrxAPI::pushData public function Push data onto the Stack
FrxAPI::report public function Run a report with a particular format. 1
FrxAPI::reportFileSystem public function Get the current report file system.
FrxAPI::setDataContext public function Set Data context by id.
FrxAPI::setDocument public function Change to a specific document type.
FrxAPI::skins public function Get list of skins.
FrxTestCase::initParametersForm public function
FrxTestCase::setUp public function Mock object instantiation. Overrides UnitTestCase::setUp 2
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
ReportTest::$doc private property
ReportTest::$r private property
ReportTest::testIncludedReport public function
ReportTest::testParameterTypes public function
ReportTest::testReportParse public function Ensure that report object can be created.
ReportTest::testSimpleRender public function Simple Render Test
ReportTest::testSimpleReport public function Test a simple report.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.