Index: tests/phpunit/tests/admin/includesMisc.php
===================================================================
--- tests/phpunit/tests/admin/includesMisc.php	(revision 26943)
+++ tests/phpunit/tests/admin/includesMisc.php	(working copy)
@@ -22,4 +22,435 @@
 		foreach ( $tests as $k => $v )
 			$this->assertEquals( $v, url_shorten( $k ) );
 	}
+
+	function test_extract_from_markers_returns_empty_array_when_file_not_found() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to return false when the function 'exists' is called
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( false ) );
+
+		$results = extract_from_markers( 'file', 'marker_1', $mock_wp_filesystem );
+
+		$this->assertEmpty( $results );
+	}
+
+	function test_extract_from_markers_returns_empty_array_for_unfound_marker() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$file_contents_without_unfound_marker = array(
+			"# BEGIN marker_10\n",
+			"marker_10 line 1\n",
+			"marker_10 line 2\n",
+			"marker_10 line 3\n",
+			"# END marker_10\n",
+			"\n",
+			"# BEGIN marker_1\n",
+			"marker_1 line 1\n",
+			"marker_1 line 2\n",
+			"marker_1 line 3\n",
+			"# END marker_1\n"
+		);
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file exists, then return $file_contents_without_unfound_marker as the file contents.
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) );
+		$mock_wp_filesystem->expects( $this->any() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents_without_unfound_marker ) );
+
+		$results = extract_from_markers( 'file', 'unfound_marker', $mock_wp_filesystem );
+
+		$this->assertEmpty( $results );
+	}
+
+	/**
+	 * This test checks that the markers are checked based on the full string.
+	 *
+	 * In the test, we create two marker sections, first 'marker_10', then 'marker_1'.
+	 * If extract_from_markers is working properly, passing in 'marker_1' will return
+	 * the contents between '# BEGIN marker_1' and '# END marker_1'.
+	 */
+	function test_extract_from_markers_checks_full_marker_line() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$file_contents_with_similar_markers = array(
+			"# BEGIN marker_10\n",
+			"marker_10 line 1\n",
+			"marker_10 line 2\n",
+			"marker_10 line 3\n",
+			"# END marker_10\n",
+			"\n",
+			"# BEGIN marker_1\n",
+			"marker_1 line 1\n",
+			"marker_1 line 2\n",
+			"marker_1 line 3\n",
+			"# END marker_1\n"
+		);
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file exists, then return $file_contents_with_similar_markers as the file contents.
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) );
+		$mock_wp_filesystem->expects( $this->any() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents_with_similar_markers ) );
+
+		$results = extract_from_markers( 'file', 'marker_1', $mock_wp_filesystem );
+
+		$expected = array(
+			"marker_1 line 1",
+			"marker_1 line 2",
+			"marker_1 line 3"
+		);
+
+		$this->assertEquals( $expected, $results );
+	}
+
+	function test_extract_from_markers_strips_newline_characters() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$file_contents_with_newline_char = array(
+			"# BEGIN marker_1\n",
+			"marker_1 line 1\n",
+			"marker_1 line 2\n",
+			"marker_1 line 3\n",
+			"# END marker_1\n",
+			"\n",
+			"# BEGIN marker_2\n",
+			"marker_2 line 1\n",
+			"marker_2 line 2\n",
+			"marker_2 line 3\n",
+			"# END marker_2\n"
+		);
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file exists, then return $file_contents_with_newline_char as the file contents.
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) );
+		$mock_wp_filesystem->expects( $this->any() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents_with_newline_char ) );
+
+		$results = extract_from_markers( 'file', 'marker_1', $mock_wp_filesystem );
+
+		$expected = array(
+			"marker_1 line 1",
+			"marker_1 line 2",
+			"marker_1 line 3"
+		);
+
+		$this->assertEquals( $expected, $results );
+	}
+
+	function test_extract_from_markers_strips_carriage_returns() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$file_contents_with_carriage_return = array(
+			"# BEGIN marker_1\r",
+			"marker_1 line 1\r",
+			"marker_1 line 2\r",
+			"marker_1 line 3\r",
+			"# END marker_1\r",
+			"\r",
+			"# BEGIN marker_2\r",
+			"marker_2 line 1\r",
+			"marker_2 line 2\r",
+			"marker_2 line 3\r",
+			"# END marker_2\r"
+		);
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file exists, then return $file_contents_with_carriage_return as the file contents.
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) );
+		$mock_wp_filesystem->expects( $this->any() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents_with_carriage_return ) );
+
+		$results = extract_from_markers( 'file', 'marker_2', $mock_wp_filesystem );
+
+		$expected = array(
+			"marker_2 line 1",
+			"marker_2 line 2",
+			"marker_2 line 3"
+		);
+
+		$this->assertEquals( $expected, $results );
+	}
+
+	function test_extract_from_markers_strips_carriage_returns_and_newline_characters() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$file_contents_with_carriage_return_and_newline_char = array(
+			"# BEGIN marker_1\r\n",
+			"marker_1 line 1\r\n",
+			"marker_1 line 2\r\n",
+			"marker_1 line 3\r\n",
+			"# END marker_1\r\n",
+			"\r\n",
+			"# BEGIN marker_2\r\n",
+			"marker_2 line 1\r\n",
+			"marker_2 line 2\r\n",
+			"marker_2 line 3\r\n",
+			"# END marker_2\r\n"
+		);
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file exists, then return $file_contents_with_carriage_return_and_newline_char as the file contents.
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) );
+		$mock_wp_filesystem->expects( $this->any() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents_with_carriage_return_and_newline_char ) );
+
+		$results = extract_from_markers( 'file', 'marker_1', $mock_wp_filesystem );
+
+		$expected = array(
+			"marker_1 line 1",
+			"marker_1 line 2",
+			"marker_1 line 3"
+		);
+
+		$this->assertEquals( $expected, $results );
+
+	}
+
+	function test_insert_with_markers_returns_false_when_file_exists_but_is_not_writable() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file exists, but is not writable
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) );
+		$mock_wp_filesystem->expects( $this->once() )->method( 'is_writable' )->will( $this->returnValue( false ) );
+
+		$insertion = array( '1', '2', '3' );
+
+		$results = insert_with_markers( 'file', 'marker', $insertion, $mock_wp_filesystem );
+
+		$this->assertFalse( $results );
+	}
+
+	function test_insert_with_markers_formats_insertion_properly() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$filename  = 'file';
+		$marker    = 'marker';
+		$insertion = array( '1', '2', '3' );
+
+		$expected_contents = implode( "\n", array(
+			"",
+			"# BEGIN {$marker}",
+			'1',
+			'2',
+			'3',
+			"# END {$marker}"
+		) );
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file doesn't exist
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( false ) );
+		// Set the mock WP_Filesystem_Direct object to have 'put_contents()' called with the argument $expected_contents. This is an assertion.
+		$mock_wp_filesystem->expects( $this->once() )->method( 'put_contents' )->with( $this->equalTo( $filename ), $this->equalTo( $expected_contents ) )->will( $this->returnValue( true ) );
+
+		$results = insert_with_markers( $filename, $marker, $insertion, $mock_wp_filesystem );
+
+		$this->assertTrue( $results );
+	}
+
+	function test_insert_with_markers_adds_new_marker_insertion_after_current_file_contents() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$file_contents = array(
+			"# BEGIN marker_1\n",
+			"marker_1 line 1\n",
+			"marker_1 line 2\n",
+			"marker_1 line 3\n",
+			"# END marker_1"
+		);
+
+		$filename  = 'file';
+		$marker    = 'marker';
+		$insertion = array( '1', '2', '3' );
+
+		$expected_contents = implode( "\n", array(
+			"# BEGIN marker_1",
+			"marker_1 line 1",
+			"marker_1 line 2",
+			"marker_1 line 3",
+			"# END marker_1",
+			"",
+			"# BEGIN {$marker}",
+			'1',
+			'2',
+			'3',
+			"# END {$marker}"
+		) );
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file exist and is writable.
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) );
+		$mock_wp_filesystem->expects( $this->once() )->method( 'is_writable' )->will( $this->returnValue( true ) );
+
+		// Setup the Mock WP_Filesystem_Direct object to return $file_contents when 'get_contents_array' is called
+		$mock_wp_filesystem->expects( $this->once() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents ) );
+
+		// Set the mock WP_Filesystem_Direct object to have 'put_contents()' called with the argument $expected_contents. This is an assertion.
+		$mock_wp_filesystem->expects( $this->once() )->method( 'put_contents' )->with( $this->equalTo( $filename ), $this->equalTo( $expected_contents ) )->will( $this->returnValue( true ) );
+
+		$results = insert_with_markers( $filename, $marker, $insertion, $mock_wp_filesystem );
+
+		$this->assertTrue( $results );
+	}
+
+	function test_insert_with_markers_overwrites_specified_marker_section_with_insertion() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$file_contents = array(
+			"# BEGIN marker_1\n",
+			"marker_1 line 1\n",
+			"marker_1 line 2\n",
+			"marker_1 line 3\n",
+			"# END marker_1\n",
+			"\n",
+			"# BEGIN marker\n",
+			"marker line 1\n",
+			"marker line 2\n",
+			"marker line 3\n",
+			"# END marker"
+		);
+
+		$filename  = 'file';
+		$marker    = 'marker';
+		$insertion = array( '1', '2', '3' );
+
+		$expected_contents = implode( "\n", array(
+			"# BEGIN marker_1",
+			"marker_1 line 1",
+			"marker_1 line 2",
+			"marker_1 line 3",
+			"# END marker_1",
+			"",
+			"# BEGIN {$marker}",
+			'1',
+			'2',
+			'3',
+			"# END {$marker}"
+		) );
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file exist and is writable.
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) );
+		$mock_wp_filesystem->expects( $this->once() )->method( 'is_writable' )->will( $this->returnValue( true ) );
+
+		// Setup the Mock WP_Filesystem_Direct object to return $file_contents when 'get_contents_array' is called
+		$mock_wp_filesystem->expects( $this->once() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents ) );
+
+		// Set the mock WP_Filesystem_Direct object to have 'put_contents()' called with the argument $expected_contents. This is an assertion.
+		$mock_wp_filesystem->expects( $this->once() )->method( 'put_contents' )->with( $this->equalTo( $filename ), $this->equalTo( $expected_contents ) )->will( $this->returnValue( true ) );
+
+		$results = insert_with_markers( $filename, $marker, $insertion, $mock_wp_filesystem );
+
+		$this->assertTrue( $results );
+	}
+
+	function test_insert_with_markers_strips_newline_characters_and_or_carriage_returns() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$file_contents = array(
+			"# BEGIN marker_1\r\n",
+			"marker_1 line 1\n",
+			"marker_1 line 2\n",
+			"marker_1 line 3\n",
+			"# END marker_1\r",
+			"\n",
+			"# BEGIN marker\r",
+			"marker line 1\n",
+			"marker line 2\n",
+			"marker line 3\n",
+			"# END marker"
+		);
+
+		$filename  = 'file';
+		$marker    = 'marker';
+		$insertion = array( '1', '2', '3' );
+
+		$expected_contents = implode( "\n", array(
+			"# BEGIN marker_1",
+			"marker_1 line 1",
+			"marker_1 line 2",
+			"marker_1 line 3",
+			"# END marker_1",
+			"",
+			"# BEGIN {$marker}",
+			'1',
+			'2',
+			'3',
+			"# END {$marker}"
+		) );
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file exist and is writable.
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( true ) );
+		$mock_wp_filesystem->expects( $this->once() )->method( 'is_writable' )->will( $this->returnValue( true ) );
+
+		// Setup the Mock WP_Filesystem_Direct object to return $file_contents when 'get_contents_array' is called
+		$mock_wp_filesystem->expects( $this->once() )->method( 'get_contents_array' )->will( $this->returnValue( $file_contents ) );
+
+		// Set the mock WP_Filesystem_Direct object to have 'put_contents()' called with the argument $expected_contents. This is an assertion.
+		$mock_wp_filesystem->expects( $this->once() )->method( 'put_contents' )->with( $this->equalTo( $filename ), $this->equalTo( $expected_contents ) )->will( $this->returnValue( true ) );
+
+		$results = insert_with_markers( $filename, $marker, $insertion, $mock_wp_filesystem );
+
+		$this->assertTrue( $results );
+	}
+
+	function test_insert_with_markers_returns_false_when_writing_to_file_fails() {
+
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-base.php' );
+		require_once( ABSPATH . 'wp-admin/includes/class-wp-filesystem-direct.php' );
+
+		$filename  = 'file';
+		$marker    = 'marker';
+		$insertion = array( '1', '2', '3' );
+
+		$expected_contents = implode( "\n", array(
+			"",
+			"# BEGIN {$marker}",
+			'1',
+			'2',
+			'3',
+			"# END {$marker}"
+		) );
+
+		$mock_wp_filesystem = $this->getMockBuilder( 'WP_Filesystem_Direct' )->disableOriginalConstructor()->getMock();
+
+		// Set the mock WP_Filesystem_Direct object to say the file doesn't exist
+		$mock_wp_filesystem->expects( $this->any() )->method( 'exists' )->will( $this->returnValue( false ) );
+		// Set the mock WP_Filesystem_Direct object to have 'put_contents()' called with the argument $expected_contents
+		$mock_wp_filesystem->expects( $this->once() )->method( 'put_contents' )->with( $this->equalTo( $filename ), $this->equalTo( $expected_contents ) )->will( $this->returnValue( false ) );
+
+		$results = insert_with_markers( $filename, $marker, $insertion, $mock_wp_filesystem );
+
+		$this->assertFalse( $results );
+	}
 }
