diff --git a/includes/admin/functions.php b/includes/admin/functions.php index 65727cf6e2a..7abfcbb3c8a 100644 --- a/includes/admin/functions.php +++ b/includes/admin/functions.php @@ -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' ); diff --git a/includes/options/views/class-amp-analytics-options-serializer.php b/includes/options/views/class-amp-analytics-options-serializer.php index aa65740d9df..6838bb4f822 100644 --- a/includes/options/views/class-amp-analytics-options-serializer.php +++ b/includes/options/views/class-amp-analytics-options-serializer.php @@ -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'; @@ -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 diff --git a/includes/options/views/class-amp-analytics-options-submenu-page.php b/includes/options/views/class-amp-analytics-options-submenu-page.php index 00865f23287..9a382d801eb 100644 --- a/includes/options/views/class-amp-analytics-options-submenu-page.php +++ b/includes/options/views/class-amp-analytics-options-submenu-page.php @@ -16,7 +16,7 @@ private function render_option($id = "", $type = "", $config = "") { ?>
-

Analytics Component:

+

Analytics Component:

diff --git a/tests/test-amp-analytics-options.php b/tests/test-amp-analytics-options.php new file mode 100644 index 00000000000..2560ccab93e --- /dev/null +++ b/tests/test-amp-analytics-options.php @@ -0,0 +1,104 @@ +serializer = new Analytics_Options_Serializer(); + } + + public function tearDown() { + global $wpdb; + $wpdb->query( 'ROLLBACK' ); + } + + public function get_analytics_component_data() { + return array( + 'one_component' => array( '', '') + ); + } + + 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) ); + } +}