<commit_before><?php
/**
 * Tests of the parsing methods within mf2\Parser
 */

namespace mf2\Parser\test;

// Include Parser.php
$autoloader = require_once dirname(__DIR__) . '/../mf2/Parser.php';

use mf2\Parser,
	PHPUnit_Framework_TestCase,
	DateTime;

class ParseImpliedTest extends PHPUnit_Framework_TestCase
{	
	public function setUp()
	{
		date_default_timezone_set('Europe/London');
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedPNameFromNodeValue()
	{
		$input = '<span class="h-card">The Name</span>';
		$parser = new Parser($input);
		$output = $parser -> parse();
		
		$this -> assertArrayHasKey('name', $output['items'][0]['properties']);
		$this -> assertEquals('The Name', $output['items'][0]['properties']['name'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedPNameFromImgAlt()
	{
		$input = '<img class="h-card" src="" alt="The Name" />';
		$parser = new Parser($input);
		$output = $parser -> parse();
		
		$this -> assertArrayHasKey('name', $output['items'][0]['properties']);
		$this -> assertEquals('The Name', $output['items'][0]['properties']['name'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedPNameFromNestedImgAlt()
	{
		$input = '<div class="h-card"><img src="" alt="The Name" /></div>';
		$parser = new Parser($input);
		$output = $parser -> parse();
		
		$this -> assertArrayHasKey('name', $output['items'][0]['properties']);
		$this -> assertEquals('The Name', $output['items'][0]['properties']['name'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedPNameFromDoublyNestedImgAlt()
	{
		$input = '<div class="h-card"><span><img src="" alt="The Name" /></span></div>';
		$parser = new Parser($input);
		$output = $parser -> parse();
		
		$this -> assertArrayHasKey('name', $output['items'][0]['properties']);
		$this -> assertEquals('The Name', $output['items'][0]['properties']['name'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedUPhotoFromImgSrc()
	{
		$input = '<img class="h-card" src="http://example.com/img.png" alt="" />';
		$parser = new Parser($input);
		$output = $parser -> parse();
		
		$this -> assertArrayHasKey('photo', $output['items'][0]['properties']);
		$this -> assertEquals('http://example.com/img.png', $output['items'][0]['properties']['photo'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedUPhotoFromNestedImgSrc()
	{
		$input = '<div class="h-card"><img src="http://example.com/img.png" alt="" /></div>';
		$parser = new Parser($input);
		$output = $parser -> parse();
				
		$this -> assertArrayHasKey('photo', $output['items'][0]['properties']);
		$this -> assertEquals('http://example.com/img.png', $output['items'][0]['properties']['photo'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedUPhotoFromDoublyNestedImgSrc()
	{
		$input = '<div class="h-card"><span><img src="http://example.com/img.png" alt="" /></span></div>';
		$parser = new Parser($input);
		$output = $parser -> parse();
		
		$this -> assertArrayHasKey('photo', $output['items'][0]['properties']);
		$this -> assertEquals('http://example.com/img.png', $output['items'][0]['properties']['photo'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedUUrlFromAHref()
	{
		$input = '<a class="h-card" href="http://example.com/">Some Name</a>';
		$parser = new Parser($input);
		$output = $parser -> parse();
		
		$this -> assertArrayHasKey('url', $output['items'][0]['properties']);
		$this -> assertEquals('http://example.com/', $output['items'][0]['properties']['url'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedUUrlFromNestedAHref()
	{
		$input = '<span class="h-card"><a href="http://example.com/">Some Name</a></span>';
		$parser = new Parser($input);
		$output = $parser -> parse();
		
		$this -> assertArrayHasKey('url', $output['items'][0]['properties']);
		$this -> assertEquals('http://example.com/', $output['items'][0]['properties']['url'][0]);
	}
	
	public function testMultipleImpliedHCards()
	{
		$input = '<span class="h-card"><NAME></span>
 
<a class="h-card" href="http://benward.me"><NAME></a>
 
<img class="h-card" alt="Sally Ride" 
	 src="http://upload.wikimedia.org/wikipedia/commons/a/a4/Ride-s.jpg"/>
 
<a class="h-card" href="http://tantek.com">
 <img alt="Tantek Çelik" src="http://ttk.me/logo.jpg"/>
</a>';
		$expected = '{
	"items": [{
		"type": ["h-card"],
		"properties": {
			"name": ["<NAME>"]
		}
	},
	{
		"type": ["h-card"],
		"properties": {
			"name": ["<NAME>"],
			"url": ["http://benward.me"]
		}
	},
	{
		"type": ["h-card"],
		"properties": {
			"name": ["<NAME>"],
			"photo": ["http://upload.wikimedia.org/wikipedia/commons/a/a4/Ride-s.jpg"]
		}
	},
	{
		"type": ["h-card"],
		"properties": {
			"name": ["<NAME>"],
			"url": ["http://tantek.com"],
			"photo": ["http://ttk.me/logo.jpg"]
		}
	}]
}';
		
		$parser = new Parser($input);
		$output = $parser -> parse();
		
		$this -> assertJsonStringEqualsJsonString(json_encode($output), $expected);
	}
}

// EOF tests/mf2/testParser.php<commit_msg>Wrote failing tests for incorrect implied photo parsing behaviour<commit_after><?php
/**
 * Tests of the parsing methods within mf2\Parser
 */

namespace mf2\Parser\test;

// Include Parser.php
$autoloader = require_once dirname(__DIR__) . '/../mf2/Parser.php';

use mf2\Parser,
	PHPUnit_Framework_TestCase,
	DateTime;

class ParseImpliedTest extends PHPUnit_Framework_TestCase
{	
	public function setUp()
	{
		date_default_timezone_set('Europe/London');
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedPNameFromNodeValue()
	{
		$input = '<span class="h-card">The Name</span>';
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertArrayHasKey('name', $output['items'][0]['properties']);
		$this->assertEquals('The Name', $output['items'][0]['properties']['name'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedPNameFromImgAlt()
	{
		$input = '<img class="h-card" src="" alt="The Name" />';
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertArrayHasKey('name', $output['items'][0]['properties']);
		$this->assertEquals('The Name', $output['items'][0]['properties']['name'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedPNameFromNestedImgAlt()
	{
		$input = '<div class="h-card"><img src="" alt="The Name" /></div>';
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertArrayHasKey('name', $output['items'][0]['properties']);
		$this->assertEquals('The Name', $output['items'][0]['properties']['name'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedPNameFromDoublyNestedImgAlt()
	{
		$input = '<div class="h-card"><span><img src="" alt="The Name" /></span></div>';
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertArrayHasKey('name', $output['items'][0]['properties']);
		$this->assertEquals('The Name', $output['items'][0]['properties']['name'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedUPhotoFromImgSrc()
	{
		$input = '<img class="h-card" src="http://example.com/img.png" alt="" />';
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertArrayHasKey('photo', $output['items'][0]['properties']);
		$this->assertEquals('http://example.com/img.png', $output['items'][0]['properties']['photo'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedUPhotoFromNestedImgSrc()
	{
		$input = '<div class="h-card"><img src="http://example.com/img.png" alt="" /></div>';
		$parser = new Parser($input);
		$output = $parser->parse();
				
		$this->assertArrayHasKey('photo', $output['items'][0]['properties']);
		$this->assertEquals('http://example.com/img.png', $output['items'][0]['properties']['photo'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedUPhotoFromDoublyNestedImgSrc()
	{
		$input = '<div class="h-card"><span><img src="http://example.com/img.png" alt="" /></span></div>';
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertArrayHasKey('photo', $output['items'][0]['properties']);
		$this->assertEquals('http://example.com/img.png', $output['items'][0]['properties']['photo'][0]);
	}
	
	public function testIgnoresImgIfNotOnlyChild() {
		$input = '<div class="h-card"><img src="http://example.com/img.png" /> <p>Moar text</p></div>';
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertArrayNotHasKey('photo', $output['items'][0]['properties']);
	}
	
	public function testIgnoresDoublyNestedImgIfNotOnlyDoublyNestedChild() {
		$input = '<div class="h-card"><span><img src="http://example.com/img.png" /> <p>Moar text</p></span></div>';
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertArrayNotHasKey('photo', $output['items'][0]['properties']);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedUUrlFromAHref()
	{
		$input = '<a class="h-card" href="http://example.com/">Some Name</a>';
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertArrayHasKey('url', $output['items'][0]['properties']);
		$this->assertEquals('http://example.com/', $output['items'][0]['properties']['url'][0]);
	}
	
	/**
	 * @group parseH
	 * @group implied
	 */
	public function testParsesImpliedUUrlFromNestedAHref()
	{
		$input = '<span class="h-card"><a href="http://example.com/">Some Name</a></span>';
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertArrayHasKey('url', $output['items'][0]['properties']);
		$this->assertEquals('http://example.com/', $output['items'][0]['properties']['url'][0]);
	}
	
	public function testMultipleImpliedHCards()
	{
		$input = '<span class="h-card"><NAME></span>
 
<a class="h-card" href="http://benward.me"><NAME></a>
 
<img class="h-card" alt="<NAME>" 
	 src="http://upload.wikimedia.org/wikipedia/commons/a/a4/Ride-s.jpg"/>
 
<a class="h-card" href="http://tantek.com">
 <img alt="Tantek Çelik" src="http://ttk.me/logo.jpg"/>
</a>';
		$expected = '{
	"items": [{
		"type": ["h-card"],
		"properties": {
			"name": ["<NAME>"]
		}
	},
	{
		"type": ["h-card"],
		"properties": {
			"name": ["<NAME>"],
			"url": ["http://benward.me"]
		}
	},
	{
		"type": ["h-card"],
		"properties": {
			"name": ["<NAME>"],
			"photo": ["http://upload.wikimedia.org/wikipedia/commons/a/a4/Ride-s.jpg"]
		}
	},
	{
		"type": ["h-card"],
		"properties": {
			"name": ["<NAME>"],
			"url": ["http://tantek.com"],
			"photo": ["http://ttk.me/logo.jpg"]
		}
	}]
}';
		
		$parser = new Parser($input);
		$output = $parser->parse();
		
		$this->assertJsonStringEqualsJsonString(json_encode($output), $expected);
	}
}

// EOF tests/mf2/testParser.php
{'fraction_non_alphanumeric': 0.23761575114627348, 'fraction_numerical': 0.0062932662051604785, 'mean_word_length': 4.493333333333333, 'pattern_counts': {'":': 34, '<': 98, '<?xml version=': 0, '>': 158, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2862098', 'n_tokens_mistral': 4153, 'n_tokens_neox': 3834, 'n_words': 643}