Skip to content

Commit

Permalink
Resolve disk read issue in E2E test, and add PHPUnit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jakejackson1 committed Feb 19, 2024
1 parent 93e7b8a commit a351d25
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 18 deletions.
19 changes: 19 additions & 0 deletions src/Helper/Helper_Templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,18 @@ public function get_template_url() {
* @since 4.1
*/
public function get_all_templates() {
$options = GPDFAPI::get_options_class();
$debug = $options->get_option( 'debug_mode', 'No' );

if ( $debug === 'No' ) {
$cache_name = $this->data->template_transient_cache . '-template-list';
$cache = get_transient( $cache_name );

/* There may be no transient and we got a non-array. If that occurs reset $cache */
if ( is_array( $cache ) && ! empty( $cache ) ) {
return $cache;
}
}

$template_list = [];
$matched_templates_basename_list = [];
Expand All @@ -132,6 +144,12 @@ function( $file ) {
);
}

if ( $debug === 'No' ) {
$cache = $template_list;

set_transient( $cache_name, $cache, 604800 );
}

return $template_list;
}

Expand Down Expand Up @@ -427,6 +445,7 @@ public function get_template_info_by_path( $template_path, $cache_name = '', $ca
*/
public function flush_template_transient_cache() {
delete_transient( $this->data->template_transient_cache );
delete_transient( $this->data->template_transient_cache . '-template-list' );
}

/**
Expand Down
6 changes: 4 additions & 2 deletions src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -406,8 +406,10 @@ private function register_scripts() {
wp_register_script( 'gfpdf_js_entries', PDF_PLUGIN_URL . 'dist/assets/js/gfpdf-entries.min.js', [ 'jquery' ], $version, true );

/* Localise admin script */
wp_localize_script( 'gfpdf_js_entrypoint', 'GFPDF', $this->data->get_localised_script_data( $this->options, $this->gform ) );
wp_localize_script( 'gfpdf_js_settings', 'GFPDF', $this->data->get_localised_script_data( $this->options, $this->gform ) );
$data = $this->data->get_localised_script_data( $this->options, $this->gform );

Check warning on line 409 in src/bootstrap.php

View check run for this annotation

Codecov / codecov/patch

src/bootstrap.php#L409

Added line #L409 was not covered by tests

wp_localize_script( 'gfpdf_js_entrypoint', 'GFPDF', $data );
wp_localize_script( 'gfpdf_js_settings', 'GFPDF', $data );

Check warning on line 412 in src/bootstrap.php

View check run for this annotation

Codecov / codecov/patch

src/bootstrap.php#L411-L412

Added lines #L411 - L412 were not covered by tests
}

/**
Expand Down
26 changes: 19 additions & 7 deletions tests/phpunit/unit-tests/test-helper-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,39 +105,51 @@ public function test_get_all_templates() {
$templates = $this->templates->get_all_templates();

/* Test the standard templates */
$this->assertEquals( 4, count( $templates ) );
$this->assertCount( 4, $templates );

/* Test for additional templates in PDF working directory */
touch( $gfpdf->data->template_location . 'test.php' );
touch( $gfpdf->data->template_location . 'test2.php' );

delete_transient( $gfpdf->data->template_transient_cache . '-template-list' );

$templates = $this->templates->get_all_templates();
$this->assertEquals( 6, count( $templates ) );
$this->assertCount( 6, $templates );

/* Test for override */
$templates = $this->templates->get_all_templates();
touch( $gfpdf->data->template_location . 'zadani.php' );

$this->assertEquals( 6, count( $templates ) );
delete_transient( $gfpdf->data->template_transient_cache . '-template-list' );

$templates = $this->templates->get_all_templates();

$this->assertCount( 6, $templates );

/* Check that a configuration.php or configuration.archive.php file don't count */
touch( $gfpdf->data->template_location . 'configuration.php' );
touch( $gfpdf->data->template_location . 'configuration.archive.php' );

delete_transient( $gfpdf->data->template_transient_cache . '-template-list' );

$templates = $this->templates->get_all_templates();
$this->assertEquals( 6, count( $templates ) );
$this->assertCount( 6, $templates );

/* Test for multisite templates */
if ( is_multisite() ) {
touch( $gfpdf->data->multisite_template_location . 'test3.php' );

delete_transient( $gfpdf->data->template_transient_cache . '-template-list' );

$templates = $this->templates->get_all_templates();
$this->assertEquals( 7, count( $templates ) );
$this->assertCount( 7, $templates );

/* Check for override */
touch( $gfpdf->data->multisite_template_location . 'zadani.php' );

delete_transient( $gfpdf->data->template_transient_cache . '-template-list' );

$templates = $this->templates->get_all_templates();
$this->assertEquals( 7, count( $templates ) );
$this->assertCount( 7, $templates );
}
}

Expand Down
60 changes: 51 additions & 9 deletions tests/phpunit/unit-tests/test-pdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,10 @@ public function test_middle_user_capability() {
*/
public function test_view_pdf_entry_list() {

$user = $this::factory()->user->create_and_get();
$user->add_role( 'administrator' );
wp_set_current_user( $user->ID );

$results = $this->create_form_and_entries();
$form_id = $results['form']['id'];
$entry = $results['entry'];
Expand All @@ -717,6 +721,8 @@ public function test_view_pdf_entry_list() {
$html = ob_get_clean();

$this->assertNotFalse( strpos( $html, 'View PDFs</a>' ) );

wp_set_current_user( 0 );
}

/**
Expand All @@ -736,25 +742,61 @@ public function test_get_pdf_display_list() {

$pdfs = $this->model->get_pdf_display_list( $entry );

$this->assertArrayHasKey( 'name', $pdfs[0] );
$this->assertArrayHasKey( 'view', $pdfs[0] );
$this->assertArrayHasKey( 'download', $pdfs[0] );

$this->assertNotFalse( strpos( $pdfs[0]['name'], 'test-' ) );
$this->assertNotFalse( strpos( $pdfs[0]['view'], 'http://example.org/?gpdf=1&pid=556690c67856b&lid=1' ) );
$this->assertNotFalse( strpos( $pdfs[0]['download'], 'http://example.org/?gpdf=1&pid=556690c67856b&lid=1&action=download' ) );
$this->assertStringContainsString( 'test-', $pdfs[0]['name'] );
$this->assertStringContainsString( 'http://example.org/?gpdf=1&pid=556690c67856b&lid=1', $pdfs[0]['view'] );
$this->assertStringContainsString( 'http://example.org/?gpdf=1&pid=556690c67856b&lid=1&action=download', $pdfs[0]['download'] );

/* Process fancy permalinks */
$wp_rewrite->set_permalink_structure( '/%postname%/' );
flush_rewrite_rules();

$pdfs = $this->model->get_pdf_display_list( $entry );

$this->assertNotFalse( strpos( $pdfs[0]['view'], 'http://example.org/pdf/556690c67856b/' ) );
$this->assertNotFalse( strpos( $pdfs[0]['download'], '/download/' ) );
$this->assertStringContainsString( 'http://example.org/pdf/556690c67856b/', $pdfs[0]['view'] );
$this->assertStringContainsString( '/download/', $pdfs[0]['download'] );

$wp_rewrite->set_permalink_structure( '' );
flush_rewrite_rules();
}

public function test_view_pdf_gravityflow_inbox() {
global $wp_rewrite;

$user = $this::factory()->user->create_and_get();
$user->add_role( 'administrator' );
wp_set_current_user( $user->ID );

/* Setup some test data */
$results = $this->create_form_and_entries();
$form = $results['form'];
$entry = $results['entry'];

$wp_rewrite->set_permalink_structure( '' );
flush_rewrite_rules();

ob_start();
$this->model->view_pdf_gravityflow_inbox( $form, $entry, [], [] );
$html = ob_get_clean();

$this->assertStringContainsString( 'http://example.org/?gpdf=1&#038;pid=fawf90c678523b&#038;lid=1', $html );
$this->assertStringContainsString( 'http://example.org/?gpdf=1&#038;pid=fawf90c678523b&#038;lid=1&#038;action=download', $html );

/* Process fancy permalinks */
$wp_rewrite->set_permalink_structure( '/%postname%/' );
flush_rewrite_rules();

ob_start();
$this->model->view_pdf_gravityflow_inbox( $form, $entry, [], [] );
$html = ob_get_clean();

$id = $entry['id'];
$this->assertStringContainsString( 'http://example.org/pdf/556690c67856b/' . $id . '/', $html );
$this->assertStringContainsString( 'http://example.org/pdf/556690c67856b/' . $id . '/download/', $html );

$wp_rewrite->set_permalink_structure( '' );
flush_rewrite_rules();

wp_set_current_user( 0 );
}

/**
Expand Down

0 comments on commit a351d25

Please sign in to comment.