Index: src/wp-includes/class-wp-network-query.php
===================================================================
--- src/wp-includes/class-wp-network-query.php	(revision 40473)
+++ src/wp-includes/class-wp-network-query.php	(working copy)
@@ -42,6 +42,24 @@
 	);
 
 	/**
+	 * Metadata query container.
+	 *
+	 * @since 4.8.0
+	 * @access public
+	 * @var object WP_Meta_Query
+	 */
+	public $meta_query = false;
+
+	/**
+	 * Metadata query clauses.
+	 *
+	 * @since 4.8.0
+	 * @access protected
+	 * @var array
+	 */
+	protected $meta_query_clauses = array();
+
+	/**
 	 * Query vars set by the user.
 	 *
 	 * @since 4.6.0
@@ -92,54 +110,73 @@
 	 * Sets up the network query, based on the query vars passed.
 	 *
 	 * @since 4.6.0
+	 * @since 4.8.0 Introduced the `update_network_meta_cache`, `meta_query`,
+	 *              `meta_key`, `meta_value`, `meta_type` and `meta_compare`
+	 *              parameters.
 	 * @access public
 	 *
 	 * @param string|array $query {
 	 *     Optional. Array or query string of network query parameters. Default empty.
 	 *
-	 *     @type array        $network__in          Array of network IDs to include. Default empty.
-	 *     @type array        $network__not_in      Array of network IDs to exclude. Default empty.
- 	 *     @type bool         $count                Whether to return a network count (true) or array of network objects.
- 	 *                                              Default false.
- 	 *     @type string       $fields               Network fields to return. Accepts 'ids' (returns an array of network IDs)
- 	 *                                              or empty (returns an array of complete network objects). Default empty.
- 	 *     @type int          $number               Maximum number of networks to retrieve. Default empty (no limit).
- 	 *     @type int          $offset               Number of networks to offset the query. Used to build LIMIT clause.
- 	 *                                              Default 0.
- 	 *     @type bool         $no_found_rows        Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
- 	 *     @type string|array $orderby              Network status or array of statuses. Accepts 'id', 'domain', 'path',
- 	 *                                              'domain_length', 'path_length' and 'network__in'. Also accepts false,
- 	 *                                              an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
- 	 *     @type string       $order                How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
- 	 *     @type string       $domain               Limit results to those affiliated with a given domain. Default empty.
- 	 *     @type array        $domain__in           Array of domains to include affiliated networks for. Default empty.
- 	 *     @type array        $domain__not_in       Array of domains to exclude affiliated networks for. Default empty.
- 	 *     @type string       $path                 Limit results to those affiliated with a given path. Default empty.
- 	 *     @type array        $path__in             Array of paths to include affiliated networks for. Default empty.
- 	 *     @type array        $path__not_in         Array of paths to exclude affiliated networks for. Default empty.
- 	 *     @type string       $search               Search term(s) to retrieve matching networks for. Default empty.
-	 *     @type bool         $update_network_cache Whether to prime the cache for found networks. Default true.
+	 *     @type array        $network__in               Array of network IDs to include. Default empty.
+	 *     @type array        $network__not_in           Array of network IDs to exclude. Default empty.
+ 	 *     @type bool         $count                     Whether to return a network count (true) or array of network objects.
+ 	 *                                                   Default false.
+ 	 *     @type string       $fields                    Network fields to return. Accepts 'ids' (returns an array of network IDs)
+ 	 *                                                   or empty (returns an array of complete network objects). Default empty.
+ 	 *     @type int          $number                    Maximum number of networks to retrieve. Default empty (no limit).
+ 	 *     @type int          $offset                    Number of networks to offset the query. Used to build LIMIT clause.
+ 	 *                                                   Default 0.
+ 	 *     @type bool         $no_found_rows             Whether to disable the `SQL_CALC_FOUND_ROWS` query. Default true.
+ 	 *     @type string|array $orderby                   Network status or array of statuses. Accepts 'id', 'domain', 'path',
+ 	 *                                                   'domain_length', 'path_length' and 'network__in'. Also accepts false,
+ 	 *                                                   an empty array, or 'none' to disable `ORDER BY` clause. Default 'id'.
+ 	 *     @type string       $order                     How to order retrieved networks. Accepts 'ASC', 'DESC'. Default 'ASC'.
+ 	 *     @type string       $domain                    Limit results to those affiliated with a given domain. Default empty.
+ 	 *     @type array        $domain__in                Array of domains to include affiliated networks for. Default empty.
+ 	 *     @type array        $domain__not_in            Array of domains to exclude affiliated networks for. Default empty.
+ 	 *     @type string       $path                      Limit results to those affiliated with a given path. Default empty.
+ 	 *     @type array        $path__in                  Array of paths to include affiliated networks for. Default empty.
+ 	 *     @type array        $path__not_in              Array of paths to exclude affiliated networks for. Default empty.
+ 	 *     @type string       $search                    Search term(s) to retrieve matching networks for. Default empty.
+	 *     @type bool         $update_network_cache      Whether to prime the cache for found networks. Default true.
+	 *     @type bool         $update_network_meta_cache Whether to prime meta caches for found networks. Default true.
+	 *     @type array        $meta_query                Optional. Meta query clauses to limit retrieved networks by.
+	 *                                                   See `WP_Meta_Query`. Default empty.
+	 *     @type string       $meta_key                  Limit networks to those matching a specific metadata key.
+	 *                                                   Can be used in conjunction with `$meta_value`. Default empty.
+	 *     @type string       $meta_value                Limit networks to those matching a specific metadata value.
+	 *                                                   Usually used in conjunction with `$meta_key`. Default empty.
+	 *     @type string       $meta_type                 Type of object metadata is for (e.g., comment, post, or user).
+	 *                                                   Default empty.
+	 *     @type string       $meta_compare              Comparison operator to test the 'meta_value'. Default empty.
 	 * }
 	 */
 	public function __construct( $query = '' ) {
 		$this->query_var_defaults = array(
-			'network__in'          => '',
-			'network__not_in'      => '',
-			'count'                => false,
-			'fields'               => '',
-			'number'               => '',
-			'offset'               => '',
-			'no_found_rows'        => true,
-			'orderby'              => 'id',
-			'order'                => 'ASC',
-			'domain'               => '',
-			'domain__in'           => '',
-			'domain__not_in'       => '',
-			'path'                 => '',
-			'path__in'             => '',
-			'path__not_in'         => '',
-			'search'               => '',
-			'update_network_cache' => true,
+			'network__in'               => '',
+			'network__not_in'           => '',
+			'count'                     => false,
+			'fields'                    => '',
+			'number'                    => '',
+			'offset'                    => '',
+			'no_found_rows'             => true,
+			'orderby'                   => 'id',
+			'order'                     => 'ASC',
+			'domain'                    => '',
+			'domain__in'                => '',
+			'domain__not_in'            => '',
+			'path'                      => '',
+			'path__in'                  => '',
+			'path__not_in'              => '',
+			'search'                    => '',
+			'update_network_cache'      => true,
+			'update_network_meta_cache' => true,
+			'meta_query'                => '',
+			'meta_key'                  => '',
+			'meta_value'                => '',
+			'meta_type'                 => '',
+			'meta_compare'              => '',
 		);
 
 		if ( ! empty( $query ) ) {
@@ -193,11 +230,21 @@
 	 * @since 4.6.0
 	 * @access public
 	 *
+	 * @global wpdb $wpdb WordPress database abstraction object.
+	 *
 	 * @return int|array The list of networks.
 	 */
 	public function get_networks() {
+		global $wpdb;
+
 		$this->parse_query();
 
+		// Set up meta_query so it's available to 'pre_get_networks'.
+		if ( class_exists( 'WP_Meta_Query' ) ) {
+			$this->meta_query = new WP_Meta_Query();
+			$this->meta_query->parse_query_vars( $this->query_vars );
+		}
+
 		/**
 		 * Fires before networks are retrieved.
 		 *
@@ -207,6 +254,14 @@
 		 */
 		do_action_ref_array( 'pre_get_networks', array( &$this ) );
 
+		// Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
+		if ( $this->meta_query ) {
+			$this->meta_query->parse_query_vars( $this->query_vars );
+			if ( ! empty( $this->meta_query->queries ) ) {
+				$this->meta_query_clauses = $this->meta_query->get_sql( 'site', $wpdb->site, 'id', $this );
+			}
+		}
+
 		// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
 		$key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
 		$last_changed = wp_cache_get_last_changed( 'networks' );
@@ -248,7 +303,11 @@
 		}
 
 		if ( $this->query_vars['update_network_cache'] ) {
-			_prime_network_caches( $network_ids );
+			if ( $this->meta_query ) {
+				_prime_network_caches( $network_ids, $this->query_vars['update_network_meta_cache'] );
+			} else {
+				_prime_network_caches( $network_ids, false );
+			}
 		}
 
 		// Fetch full network objects from the primed cache.
@@ -394,6 +453,17 @@
 
 		$join = '';
 
+		if ( ! empty( $this->meta_query_clauses ) ) {
+			$join .= $this->meta_query_clauses['join'];
+
+			// Strip leading 'AND'.
+			$this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );
+
+			if ( ! $this->query_vars['count'] ) {
+				$groupby = "$wpdb->site.id";
+			}
+		}
+
 		$where = implode( ' AND ', $this->sql_clauses['where'] );
 
 		$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
@@ -533,6 +603,24 @@
 			$parsed = "$wpdb->site.$orderby";
 		}
 
+		if ( ! $parsed && $this->meta_query ) {
+			switch ( $orderby ) {
+				case $this->query_vars['meta_key']:
+				case 'meta_value':
+					$parsed = "$wpdb->sitemeta.meta_value";
+					break;
+				case 'meta_value_num':
+					$parsed = "$wpdb->sitemeta.meta_value+0";
+					break;
+				default:
+					$meta_query_clauses = $this->meta_query->get_clauses();
+					if ( $meta_query_clauses && isset( $meta_query_clauses[ $orderby ] ) ) {
+						$meta_clause = $meta_query_clauses[ $orderby ];
+						$parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
+					}
+			}
+		}
+
 		return $parsed;
 	}
 
Index: src/wp-includes/load.php
===================================================================
--- src/wp-includes/load.php	(revision 40473)
+++ src/wp-includes/load.php	(working copy)
@@ -515,7 +515,7 @@
 	}
 
 	if ( function_exists( 'wp_cache_add_global_groups' ) ) {
-		wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites' ) );
+		wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'site-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site_meta' ) );
 		wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 	}
 }
Index: src/wp-includes/ms-blogs.php
===================================================================
--- src/wp-includes/ms-blogs.php	(revision 40473)
+++ src/wp-includes/ms-blogs.php	(working copy)
@@ -842,7 +842,7 @@
 			if ( is_array( $global_groups ) ) {
 				wp_cache_add_global_groups( $global_groups );
 			} else {
-				wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
+				wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'site_meta' ) );
 			}
 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 		}
@@ -916,7 +916,7 @@
 			if ( is_array( $global_groups ) ) {
 				wp_cache_add_global_groups( $global_groups );
 			} else {
-				wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
+				wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'site_meta' ) );
 			}
 			wp_cache_add_non_persistent_groups( array( 'counts', 'plugins' ) );
 		}
@@ -1153,6 +1153,116 @@
 }
 
 /**
+ * Add metadata to a network.
+ *
+ * @since 4.8.0
+ *
+ * @param int    $id         Network ID.
+ * @param string $meta_key   Metadata name.
+ * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
+ * @param bool   $unique     Optional. Whether the same key should not be added.
+ *                           Default false.
+ * @return int|false Meta ID on success, false on failure.
+ */
+function add_network_meta( $id, $meta_key, $meta_value, $unique = false ) {
+	$added = add_metadata( 'site', $id, $meta_key, $meta_value, $unique );
+
+	if ( $added ) {
+		wp_cache_set( 'last_changed', microtime(), 'networks' );
+	}
+
+	return $added;
+}
+
+/**
+ * Remove from a network, metadata matching key and/or value.
+ *
+ * You can match based on the key, or key and value. Removing based on key and
+ * value, will keep from removing duplicate metadata with the same key. It also
+ * allows removing all metadata matching key, if needed.
+ *
+ * @since 4.8.0
+ *
+ * @param int    $id         Network ID.
+ * @param string $meta_key   Metadata name.
+ * @param mixed  $meta_value Optional. Metadata value. Must be serializable if
+ *                           non-scalar. Default empty.
+ * @return bool True on success, false on failure.
+ */
+function delete_network_meta( $id, $meta_key, $meta_value = '' ) {
+	$deleted = delete_metadata( 'site', $id, $meta_key, $meta_value );
+
+	if ( $deleted ) {
+		wp_cache_set( 'last_changed', microtime(), 'networks' );
+	}
+
+	return $deleted;
+}
+
+/**
+ * Retrieve from a network, metadata value by key.
+ *
+ * @since 4.8.0
+ *
+ * @param int    $id        Network ID.
+ * @param string $meta_key  Optional. The meta key to retrieve. By default, returns
+ *                          data for all keys. Default empty.
+ * @param bool   $single    Optional. Whether to return a single value. Default false.
+ * @return mixed Will be an array if $single is false. Will be value of meta data
+ *               field if $single is true.
+ */
+function get_network_meta( $id, $meta_key = '', $single = false ) {
+	return get_metadata( 'site', $id, $meta_key, $single );
+}
+
+/**
+ * Update metadata for a network ID, and/or key, and/or value.
+ *
+ * Use the $prev_value parameter to differentiate between meta fields with the
+ * same key and network ID.
+ *
+ * If the meta field for the network does not exist, it will be added.
+ *
+ * @since 4.8.0
+ *
+ * @param int    $id         Network ID.
+ * @param string $meta_key   Metadata key.
+ * @param mixed  $meta_value Metadata value. Must be serializable if non-scalar.
+ * @param mixed  $prev_value Optional. Previous value to check before removing.
+ *                           Default empty.
+ * @return int|bool Meta ID if the key didn't exist, true on successful update,
+ *                  false on failure.
+ */
+function update_network_meta( $id, $meta_key, $meta_value, $prev_value = '' ) {
+	$updated = update_metadata( 'site', $id, $meta_key, $meta_value, $prev_value );
+
+	if ( $updated ) {
+		wp_cache_set( 'last_changed', microtime(), 'networks' );
+	}
+
+	return $updated;
+}
+
+/**
+ * Delete everything from network meta matching meta key.
+ *
+ * @since 4.8.0
+ *
+ * @param string $network_meta_key Key to search for when deleting.
+ *
+ * @return bool Whether the network meta key was deleted from the database.
+ */
+function delete_network_meta_by_key( $network_meta_key ) {
+	$deleted = delete_metadata( 'site', null, $network_meta_key, '', true );
+
+	if ( $deleted ) {
+		wp_cache_set( 'last_changed', microtime(), 'networks' );
+	}
+
+	return $deleted;
+}
+
+/**
  * Removes a network from the object cache.
  *
  * @since 4.6.0
@@ -1170,6 +1280,7 @@
 
 	foreach ( (array) $ids as $id ) {
 		wp_cache_delete( $id, 'networks' );
+		wp_cache_delete( $id, 'site_meta' );
 
 		/**
 		 * Fires immediately after a network has been removed from the object cache.
@@ -1192,34 +1303,59 @@
  * cache using the network group with the key using the ID of the networks.
  *
  * @since 4.6.0
+ * @since 4.8.0 Introduced the `$update_meta_cache` parameter.
  *
- * @param array $networks Array of network row objects.
+ * @param array $networks          Array of network row objects.
+ * @param bool  $update_meta_cache Optional. Whether to update the meta cache. Default true.
  */
-function update_network_cache( $networks ) {
+function update_network_cache( $networks, $update_meta_cache = true ) {
 	foreach ( (array) $networks as $network ) {
 		wp_cache_add( $network->id, $network, 'networks' );
 	}
+
+	if ( $update_meta_cache ) {
+		update_networkmeta_cache( wp_list_pluck( $networks, 'id' ) );
+	}
+}
+
+/**
+ * Updates metadata cache for list of network IDs.
+ *
+ * Performs SQL query to retrieve the metadata for the network IDs and updates the
+ * metadata cache for the posts. Therefore, the functions, which call this
+ * function, do not need to perform SQL queries on their own.
+ *
+ * @since 4.8.0
+ *
+ * @param array $network_ids List of network IDs.
+ * @return array|false Returns false if there is nothing to update or an array
+ *                     of metadata.
+ */
+function update_networkmeta_cache( $network_ids) {
+	return update_meta_cache( 'site', $network_ids );
 }
 
 /**
  * Adds any networks from the given IDs to the cache that do not already exist in cache.
  *
  * @since 4.6.0
+ * @since 4.8.0 Introduced the `$update_meta_cache` parameter.
  * @access private
  *
  * @see update_network_cache()
  * @global wpdb $wpdb WordPress database abstraction object.
  *
- * @param array $network_ids Array of network IDs.
+ * @param array $network_ids       Array of network IDs.
+ * @param bool  $update_meta_cache Optional. Whether to update the meta cache. Default true.
  */
-function _prime_network_caches( $network_ids ) {
+function _prime_network_caches( $network_ids, $update_meta_cache = true ) {
 	global $wpdb;
 
 	$non_cached_ids = _get_non_cached_ids( $network_ids, 'networks' );
-	if ( !empty( $non_cached_ids ) ) {
-		$fresh_networks = $wpdb->get_results( sprintf( "SELECT $wpdb->site.* FROM $wpdb->site WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
+	if ( ! empty( $non_cached_ids ) ) {
+		$fresh_networks = $wpdb->get_results( sprintf( "SELECT * FROM {$wpdb->site} WHERE id IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) );
 
-		update_network_cache( $fresh_networks );
+		update_network_cache( $fresh_networks, $update_meta_cache );
 	}
 }
 
Index: tests/phpunit/includes/testcase.php
===================================================================
--- tests/phpunit/includes/testcase.php	(revision 40473)
+++ tests/phpunit/includes/testcase.php	(working copy)
@@ -301,7 +301,7 @@
 			$wp_object_cache->__remoteset();
 		}
 		wp_cache_flush();
-		wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
+		wp_cache_add_global_groups( array( 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'site_meta' ) );
 		wp_cache_add_non_persistent_groups( array( 'comment', 'counts', 'plugins' ) );
 	}
 
Index: tests/phpunit/tests/cache.php
===================================================================
--- tests/phpunit/tests/cache.php	(revision 40473)
+++ tests/phpunit/tests/cache.php	(working copy)
@@ -22,7 +22,7 @@
 		global $wp_object_cache;
 		$cache_class = get_class( $wp_object_cache );
 		$cache = new $cache_class();
-		$cache->add_global_groups( array( 'global-cache-test', 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details' ) );
+		$cache->add_global_groups( array( 'global-cache-test', 'users', 'userlogins', 'usermeta', 'user_meta', 'useremail', 'userslugs', 'site-transient', 'site-options', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache', 'networks', 'sites', 'site-details', 'site_meta' ) );
 		return $cache;
 	}
 
Index: tests/phpunit/tests/multisite/networkMeta.php
===================================================================
--- tests/phpunit/tests/multisite/networkMeta.php	(revision 0)
+++ tests/phpunit/tests/multisite/networkMeta.php	(working copy)
@@ -0,0 +1,222 @@
+<?php
+
+if ( is_multisite() ) :
+/**
+ * @group ms-network
+ * @group multisite
+ * @group meta
+ * @ticket 25344
+ */
+class Tests_Multisite_Network_Meta extends WP_UnitTestCase {
+	protected static $network_id;
+	protected static $network_id2;
+
+	public static function wpSetUpBeforeClass( $factory ) {
+		self::$network_id = $factory->network->create( array( 'domain' => 'wordpress.org', 'path' => '/' ) );
+		self::$network_id2 = $factory->network->create( array( 'domain' => 'wordpress.org', 'path' => '/foo/' ) );
+	}
+
+	public static function wpTearDownAfterClass() {
+		global $wpdb;
+
+		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", self::$network_id ) );
+		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", self::$network_id ) );
+
+		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->sitemeta} WHERE site_id = %d", self::$network_id2 ) );
+		$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->site} WHERE id= %d", self::$network_id2 ) );
+	}
+
+	public function test_add() {
+		$this->assertNotEmpty( add_network_meta( self::$network_id, 'foo', 'bar' ) );
+	}
+
+	public function test_add_unique() {
+		$this->assertNotEmpty( add_network_meta( self::$network_id, 'foo', 'bar' ) );
+		$this->assertFalse( add_network_meta( self::$network_id, 'foo', 'bar', true ) );
+	}
+
+	public function test_delete() {
+		add_network_meta( self::$network_id, 'foo', 'bar' );
+
+		$this->assertTrue( delete_network_meta( self::$network_id, 'foo' ) );
+	}
+
+	public function test_delete_with_invalid_meta_key_should_return_false() {
+		$this->assertFalse( delete_network_meta( self::$network_id, 'foo' ) );
+	}
+
+	public function test_delete_should_respect_meta_value() {
+		add_network_meta( self::$network_id, 'foo', 'bar' );
+		add_network_meta( self::$network_id, 'foo', 'baz' );
+
+		$this->assertTrue( delete_network_meta( self::$network_id, 'foo', 'bar' ) );
+
+		$metas = get_network_meta( self::$network_id, 'foo', false );
+		$this->assertSame( array( 'baz' ), $metas );
+	}
+
+	public function test_get_with_no_key_should_fetch_all_keys() {
+		add_network_meta( self::$network_id, 'foo', 'bar' );
+		add_network_meta( self::$network_id, 'foo1', 'baz' );
+
+		$found = get_network_meta( self::$network_id );
+
+		$expected = array(
+			'foo' => array( 'bar' ),
+			'foo1' => array( 'baz' ),
+		);
+
+		// Ignore default network options.
+		$found = array_intersect_key( $found, $expected );
+
+		$this->assertEqualSets( $expected, $found );
+	}
+
+	public function test_get_with_key_should_fetch_all_for_key() {
+		add_network_meta( self::$network_id, 'foo', 'bar' );
+		add_network_meta( self::$network_id, 'foo', 'baz' );
+		add_network_meta( self::$network_id, 'foo1', 'baz' );
+
+		$found = get_network_meta( self::$network_id, 'foo' );
+		$expected = array( 'bar', 'baz' );
+
+		$this->assertEqualSets( $expected, $found );
+	}
+
+	public function test_get_should_respect_single_true() {
+		add_network_meta( self::$network_id, 'foo', 'bar' );
+		add_network_meta( self::$network_id, 'foo', 'baz' );
+
+		$found = get_network_meta( self::$network_id, 'foo', true );
+		$this->assertEquals( 'bar', $found );
+	}
+
+	public function test_update_should_pass_to_add_when_no_value_exists_for_key() {
+		$actual = update_network_meta( self::$network_id, 'foo', 'bar' );
+		$this->assertInternalType( 'int', $actual );
+		$this->assertNotEmpty( $actual );
+
+		$meta = get_network_meta( self::$network_id, 'foo', true );
+		$this->assertSame( 'bar', $meta );
+	}
+
+	public function test_update_should_return_true_when_updating_existing_value_for_key() {
+		add_network_meta( self::$network_id, 'foo', 'bar' );
+
+		$actual = update_network_meta( self::$network_id, 'foo', 'baz' );
+		$this->assertTrue( $actual );
+
+		$meta = get_network_meta( self::$network_id, 'foo', true );
+		$this->assertSame( 'baz', $meta );
+	}
+
+	public function test_delete_by_key() {
+		add_network_meta( self::$network_id, 'unique_delete_by_key', 'value', true );
+		add_network_meta( self::$network_id2, 'unique_delete_by_key', 'value', true );
+
+		$this->assertEquals( 'value', get_network_meta( self::$network_id, 'unique_delete_by_key', true ) );
+		$this->assertEquals( 'value', get_network_meta( self::$network_id2, 'unique_delete_by_key', true ) );
+
+		$this->assertTrue( delete_network_meta_by_key( 'unique_delete_by_key' ) );
+
+		$this->assertEquals( '', get_network_meta( self::$network_id, 'unique_delete_by_key', true ) );
+		$this->assertEquals( '', get_network_meta( self::$network_id2, 'unique_delete_by_key', true ) );
+	}
+
+	public function test_adding_network_meta_should_bust_get_networks_cache() {
+		add_network_meta( self::$network_id, 'foo', 'bar' );
+
+		// Prime cache.
+		$found = get_networks( array(
+			'fields' => 'ids',
+			'meta_query' => array(
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$this->assertEqualSets( array( self::$network_id ), $found );
+
+		add_network_meta( self::$network_id2, 'foo', 'bar' );
+
+		$found = get_networks( array(
+			'fields' => 'ids',
+			'meta_query' => array(
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$this->assertEqualSets( array( self::$network_id, self::$network_id2 ), $found );
+	}
+
+	public function test_updating_network_meta_should_bust_get_networks_cache() {
+		add_network_meta( self::$network_id, 'foo', 'bar' );
+		add_network_meta( self::$network_id2, 'foo', 'baz' );
+
+		// Prime cache.
+		$found = get_networks( array(
+			'fields' => 'ids',
+			'meta_query' => array(
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$this->assertEqualSets( array( self::$network_id ), $found );
+
+		update_network_meta( self::$network_id2, 'foo', 'bar' );
+
+		$found = get_networks( array(
+			'fields' => 'ids',
+			'meta_query' => array(
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$this->assertEqualSets( array( self::$network_id, self::$network_id2 ), $found );
+	}
+
+	public function test_deleting_network_meta_should_bust_get_networks_cache() {
+		add_network_meta( self::$network_id, 'foo', 'bar' );
+		add_network_meta( self::$network_id2, 'foo', 'bar' );
+
+		// Prime cache.
+		$found = get_networks( array(
+			'fields' => 'ids',
+			'meta_query' => array(
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$this->assertEqualSets( array( self::$network_id, self::$network_id2 ), $found );
+
+		delete_network_meta( self::$network_id2, 'foo', 'bar' );
+
+		$found = get_networks( array(
+			'fields' => 'ids',
+			'meta_query' => array(
+				array(
+					'key' => 'foo',
+					'value' => 'bar',
+				),
+			),
+		) );
+
+		$this->assertEqualSets( array( self::$network_id ), $found );
+	}
+}
+
+endif;

Property changes on: tests/phpunit/tests/multisite/networkMeta.php
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
