From ed994e7c7e0cdf961929efb39f51daf297843790 Mon Sep 17 00:00:00 2001
From: Fabian Todt <mail@fabiantodt.at>
Date: Fri, 3 Sep 2021 10:11:46 +0200
Subject: [PATCH] fix MockAction::get_call_count correct usage & for filters

---
 tests/phpunit/includes/utils.php                 |  7 ++++---
 tests/phpunit/tests/rest-api/rest-controller.php | 14 +++++++-------
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/tests/phpunit/includes/utils.php b/tests/phpunit/includes/utils.php
index e37c3c15d66..ce6b9a1ed8d 100644
--- a/tests/phpunit/includes/utils.php
+++ b/tests/phpunit/includes/utils.php
@@ -58,6 +58,7 @@ function strip_ws( $txt ) {
  *     add_action( 'foo', array( &$ma, 'action' ) );
  */
 class MockAction {
+	// stores all called actions/filters in an array, where `action`/`filter` is the callback and `tag` is the called hook
 	public $events;
 	public $debug;
 
@@ -168,12 +169,12 @@ function get_events() {
 		return $this->events;
 	}
 
-	// Return a count of the number of times the action was called since the last reset.
+	// Return a count of the number of times the action or filter ( = tag ) was called since the last reset.
 	function get_call_count( $tag = '' ) {
 		if ( $tag ) {
 			$count = 0;
 			foreach ( $this->events as $e ) {
-				if ( $e['action'] === $tag ) {
+				if ( $e['tag'] === $tag ) {
 					++$count;
 				}
 			}
@@ -182,7 +183,7 @@ function get_call_count( $tag = '' ) {
 		return count( $this->events );
 	}
 
-	// Return an array of the tags that triggered calls to this action.
+	// Return an array of the tags (actions or filters) that triggered calls to this action.
 	function get_tags() {
 		$out = array();
 		foreach ( $this->events as $e ) {
diff --git a/tests/phpunit/tests/rest-api/rest-controller.php b/tests/phpunit/tests/rest-api/rest-controller.php
index 0af9460aa5d..a87dd331e65 100644
--- a/tests/phpunit/tests/rest-api/rest-controller.php
+++ b/tests/phpunit/tests/rest-api/rest-controller.php
@@ -392,13 +392,13 @@ public function test_get_fields_for_response_filters_by_context() {
 
 		$controller->prepare_item_for_response( array(), $request );
 
-		$this->assertSame( 0, $listener->get_call_count( $method ) );
+		$this->assertSame( 0, $listener->get_call_count() );
 
 		$request->set_param( 'context', 'embed' );
 
 		$controller->prepare_item_for_response( array(), $request );
 
-		$this->assertGreaterThan( 0, $listener->get_call_count( $method ) );
+		$this->assertGreaterThan( 0, $listener->get_call_count() );
 	}
 
 	public function test_filtering_fields_for_response_by_context_returns_fields_with_no_context() {
@@ -426,7 +426,7 @@ public function test_filtering_fields_for_response_by_context_returns_fields_wit
 
 		$controller->prepare_item_for_response( array(), $request );
 
-		$this->assertGreaterThan( 0, $listener->get_call_count( $method ) );
+		$this->assertGreaterThan( 0, $listener->get_call_count() );
 	}
 
 	public function test_filtering_fields_for_response_by_context_returns_fields_with_no_schema() {
@@ -451,7 +451,7 @@ public function test_filtering_fields_for_response_by_context_returns_fields_wit
 
 		$controller->prepare_item_for_response( array(), $request );
 
-		$this->assertGreaterThan( 0, $listener->get_call_count( $method ) );
+		$this->assertGreaterThan( 0, $listener->get_call_count() );
 	}
 
 	/**
@@ -511,7 +511,7 @@ public function test_add_additional_fields_to_object_respects_fields_param() {
 
 		$controller->prepare_item_for_response( $item, $request );
 
-		$first_call_count = $listener->get_call_count( $method );
+		$first_call_count = $listener->get_call_count();
 
 		$this->assertGreaterThan( 0, $first_call_count );
 
@@ -519,13 +519,13 @@ public function test_add_additional_fields_to_object_respects_fields_param() {
 
 		$controller->prepare_item_for_response( $item, $request );
 
-		$this->assertSame( $first_call_count, $listener->get_call_count( $method ) );
+		$this->assertSame( $first_call_count, $listener->get_call_count() );
 
 		$request->set_param( '_fields', $field );
 
 		$controller->prepare_item_for_response( $item, $request );
 
-		$this->assertGreaterThan( $first_call_count, $listener->get_call_count( $method ) );
+		$this->assertGreaterThan( $first_call_count, $listener->get_call_count() );
 	}
 
 	/**
