Skip to content

Commit

Permalink
Add draft of test cases (not done\!)
Browse files Browse the repository at this point in the history
  • Loading branch information
amedina committed Jun 14, 2017
1 parent edeb8ce commit d25f053
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 6 deletions.
2 changes: 1 addition & 1 deletion includes/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ function amp_add_custom_analytics( ) {
$fields = get_analytics_component_fields($option);
$analytics[$fields['id']] = $fields;
}

return $analytics;
}
add_filter( 'amp_post_template_analytics', 'amp_add_custom_analytics' );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Analytics_Options_Serializer {
public function init() {
add_action( 'admin_post_analytics_options', array( $this, 'save' ) );
}

public static function save() {

$option_name = 'analytics';
Expand All @@ -22,18 +22,17 @@ public static function save() {
stripslashes($_POST['config'])
);

$inner_option_name = $_POST['vendor-type'] . '-' . $_POST['id'];

$analytics_options = get_option($option_name);
if ( ! $analytics_options ) {
$analytics_options = array();
}

$inner_option_name = $_POST['vendor-type'] . '-' . $_POST['id'];}
if ( isset($_POST['delete']) ) {
unset($analytics_options[$inner_option_name]);
} else {
$analytics_options[$inner_option_name] = $new_analytics_options;
}

update_option( $option_name, $analytics_options, false);

// [Redirect] Keep the user in the analytics options page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ private function render_option($id = "", $type = "", $config = "") {
?>
<div class="analytics-data-container">
<form method="post" action="<?php echo esc_html( admin_url( 'admin-post.php' ) ); ?>">
<h2>Analytics Component: <?php echo $type . ':' . $id ?></h2>
<h2>Analytics Component: <?php echo ($type ? $type . ':' : '') . $id ?></h2>
<div class="options">
<p>
<label>Type: </label>
Expand Down
104 changes: 104 additions & 0 deletions tests/test-amp-analytics-options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

class AMP_Analytics_Options_Test extends WP_UnitTestCase {

private $id_one = 'ga-1';
private $id_two = 'ga-2';
private $vendor = 'googleanalytics';

private $config = '{
"requests": {
"event": "https://amp-publisher-samples-staging.herokuapp.com/amp-analytics/ping?user=amp-pV356ME7W4U6b_ILVWGDfCPCqFv2m4H7mPY0SYwKQPjBFQGoGYlsYcUikw1UiDVl&account=ampbyexample&event=${eventId}"
},
"triggers": {
"trackPageview": {
"on": "visible",
"request": "event",
"visibilitySpec": {
"selector": "#cat-image-id",
"visiblePercentageMin": 20,
"totalTimeMin": 500,
"continuousTimeMin": 200
},
"vars": {
"eventId": "catview"
}
}
}
}';
private $serializer;

public function setUp() {
$this->serializer = new Analytics_Options_Serializer();
}

public function tearDown() {
global $wpdb;
$wpdb->query( 'ROLLBACK' );
}

public function get_analytics_component_data() {
return array(
'one_component' => array( '', '<amp-analytics id="googleanalytics-ga-1" type="googleanalytics"><script type="application/json">{"requests":{"event":"https:\/\/amp-publisher-samples-staging.herokuapp.com\/amp-analytics\/ping?user=amp-pV356ME7W4U6b_ILVWGDfCPCqFv2m4H7mPY0SYwKQPjBFQGoGYlsYcUikw1UiDVl&account=ampbyexample&event=${eventId}"},"triggers":{"trackPageview":{"on":"visible","request":"event","visibilitySpec":{"selector":"#cat-image-id","visiblePercentageMin":20,"totalTimeMin":500,"continuousTimeMin":200},"vars":{"eventId":"catview"}}}}</script></amp-analytics>')
);
}

private function insert_one_option($id, $vendor, $config) {
global $_POST;
$_POST['id'] = $id;
$_POST['vendor-type'] = $vendor;
$_POST['config'] = $config;
$this->serializer->save();
}

private function get_options() {
return get_option('analytics');
}

function test_no_options() {
$options = $this->get_options();
$this->assertFalse( $options );
}

function test_one_option_inserted() {
$this->insert_one_option(
$this->id_one,
$this->vendor,
$this->config
);
$options = $this->get_options();

$this->assertEquals( 1, count($options) );
}

function test_two_options_inserted() {
$this->insert_one_option(
$this->id_one,
$this->vendor,
$this->config
);
$this->insert_one_option(
$this->id_two,
$this->vendor,
$this->config
);
$options = $this->get_options();

$this->assertEquals( 2, count($options) );
}

/**
* @dataProvider get_analytics_component_data
*/
public function test__analytics_component( $source, $expected ) {
$this->insert_one_option(
$this->id_one,
$this->vendor,
$this->config
);
$dom = AMP_DOM_Utils::get_dom_from_content( $source );
$analytics_components = $dom->getElementsByTagName('amp-analytics');

$this->assertEquals( 1, count($analytics_components) );
}
}

0 comments on commit d25f053

Please sign in to comment.