<commit_before><?php
namespace Tests\Unit\Models;

use Mockery;
use Exception;
use Tests\TestCase;
use App\Models\Page;
use App\Models\Block;
use App\Models\Route;
use App\Models\Redirect;
use App\Models\PublishedPage;
use App\Http\Transformers\Api\v1\PageTransformer;
use App\Models\Definitions\Layout as LayoutDefinition;

class PageTest extends TestCase
{

	/**
	 * @test
	 * @group wip
	 */
	public function publish_WhenPageHasUnsavedChanges_ThrowsException()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$route->page->title = 'Foobar!';

		$this->expectException(Exception::class);
		$route->page->publish(new PageTransformer);
	}

	/**
	 * @test
	 * @group wip
	 */
	public function publish_WhenPageHasUnpublishedParents_ThrowsException()
	{
		$parent = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$child = factory(Route::class)->states([ 'withPage' ])->create([ 'parent_id' => $parent->getKey() ]);

		$this->expectException(Exception::class);
		$child->page->publish(new PageTransformer);
	}

	/**
	 * @test
	 * @group wip
	 */
	public function publish_CreatesPublishedPageInstance()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$count = PublishedPage::count();

		$route->page->publish(new PageTransformer);
		$this->assertEquals($count + 1, PublishedPage::count());
	}

	/**
	 * @test
	 * @group wip
	 */
	public function publish_AssociatesPublishedPageWithPageInstance()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$count = $route->page->history()->count();

		$route->page->publish(new PageTransformer);
		$this->assertEquals($count + 1, $route->page->history()->count());
		$this->assertNotNull($route->page->published);
	}

	/**
	 * @test
	 * @group wip
	 * @group integration
	 *
	 * This tests integration between $page->publish(), $route->makeActive() and $route->delete().
	 */
	public function publish_WhenThereIsAnActiveRoute_ActiveRouteGetsRedirected()
	{
		$route = factory(Route::class)->states([ 'withPage', 'withParent' ])->create();
		$route->parent->makeActive();
		$route->makeActive();

		$draft = factory(Route::class)->create([ 'page_id' => $route->page_id, 'parent_id' => $route->parent_id, 'slug' => '/foobar' ]);

		$count = Redirect::count();

		$page = $route->page->fresh();
		$page->publish(new PageTransformer);
		$this->assertEquals($count+1, Redirect::count());
	}

	/**
	 * @test
	 * @group wip
	 */
	public function publish_DraftRouteBecomesActiveRoute()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();

		$page = $route->page->fresh();
		$page->publish(new PageTransformer);

		$route = $route->fresh();
		$this->assertTrue($route->isActive());
		$this->assertEquals($route->getKey(), $route->page->activeRoute->getKey());
	}

	/**
	 * @test
	 * @group wip
	 */
	public function publish_PageOnlyHasOneRoute()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();

		$page = $route->page->fresh();
		$page->publish(new PageTransformer);

		$this->assertCount(1, $page->routes);
	}

	/**
	 * @test
	 * @group wip
	 */
	public function publish_PublishedPageBakeContainsSerializedPageInstance()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$route->page->publish(new PageTransformer);

		$json = fractal($route->page, new PageTransformer)->parseIncludes([ 'blocks', 'canonical' ])->toJson();

		$this->assertEquals($json, $route->published_page->bake);
	}



	/**
	 * @test
	 * @group wip
	 */
	public function revert_WhenPageHasUnsavedChanges_ThrowsException()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$route->page->publish(new PageTransformer);

		$route->page->title = 'Foobar!';

		$this->expectException(Exception::class);
		$route->page->revert($route->page->published);
	}

	/**
	 * @test
	 * @group wip
	 */
	public function revert_WhenPublishedPageIsNotAssociatedWithPage_ThrowsException()
	{
		$r1 = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$r1->page->publish(new PageTransformer);

		$r2 = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$r2->page->publish(new PageTransformer);

		$this->expectException(Exception::class);
		$r1->page->revert($r2->page->published);
	}

	/**
	 * @test
	 * @group wip
	 */
	public function revert_RevertsPageToMatchPublishedPage()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$page = $route->page;

		$page->publish(new PageTransformer);

		$title = $page->title;
		$page->title = 'Foobar';

		$layout = $page->layout_name;
		$page->layout_name = 'fizzbuzz17';

		$page->save();
		$this->assertEquals('Foobar', $page->title);
		$this->assertEquals('fizzbuzz17', $page->layout_name);

		$page->revert($page->published);
		$this->assertEquals($title, $page->title);
		$this->assertEquals($layout, $page->layout_name);
	}

	/**
	 * @test
	 * @group wip
	 */
	public function revert_RevertsBlocksToMatchPublishedPage()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$page = $route->page;

		$blocks = factory(Block::class, 2)->create([ 'page_id' => $page->getKey(), 'region_name' => 'test-region' ]);
		$page->publish(new PageTransformer);

		$moreBlocks = factory(Block::class, 3)->create([ 'page_id' => $page->getKey(), 'region_name' => 'test-region' ]);
<commit_msg>Removed WIP tags from Page model<commit_after><?php
namespace Tests\Unit\Models;

use Mockery;
use Exception;
use Tests\TestCase;
use App\Models\Page;
use App\Models\Block;
use App\Models\Route;
use App\Models\Redirect;
use App\Models\PublishedPage;
use App\Http\Transformers\Api\v1\PageTransformer;
use App\Models\Definitions\Layout as LayoutDefinition;

class PageTest extends TestCase
{

	/**
	 * @test
	 */
	public function publish_WhenPageHasUnsavedChanges_ThrowsException()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$route->page->title = 'Foobar!';

		$this->expectException(Exception::class);
		$route->page->publish(new PageTransformer);
	}

	/**
	 * @test
	 */
	public function publish_WhenPageHasUnpublishedParents_ThrowsException()
	{
		$parent = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$child = factory(Route::class)->states([ 'withPage' ])->create([ 'parent_id' => $parent->getKey() ]);

		$this->expectException(Exception::class);
		$child->page->publish(new PageTransformer);
	}

	/**
	 * @test
	 */
	public function publish_CreatesPublishedPageInstance()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$count = PublishedPage::count();

		$route->page->publish(new PageTransformer);
		$this->assertEquals($count + 1, PublishedPage::count());
	}

	/**
	 * @test
	 */
	public function publish_AssociatesPublishedPageWithPageInstance()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$count = $route->page->history()->count();

		$route->page->publish(new PageTransformer);
		$this->assertEquals($count + 1, $route->page->history()->count());
		$this->assertNotNull($route->page->published);
	}

	/**
	 * @test
	 * @group integration
	 *
	 * This tests integration between $page->publish(), $route->makeActive() and $route->delete().
	 */
	public function publish_WhenThereIsAnActiveRoute_ActiveRouteGetsRedirected()
	{
		$route = factory(Route::class)->states([ 'withPage', 'withParent' ])->create();
		$route->parent->makeActive();
		$route->makeActive();

		$draft = factory(Route::class)->create([ 'page_id' => $route->page_id, 'parent_id' => $route->parent_id, 'slug' => '/foobar' ]);

		$count = Redirect::count();

		$page = $route->page->fresh();
		$page->publish(new PageTransformer);
		$this->assertEquals($count+1, Redirect::count());
	}

	/**
	 * @test
	 */
	public function publish_DraftRouteBecomesActiveRoute()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();

		$page = $route->page->fresh();
		$page->publish(new PageTransformer);

		$route = $route->fresh();
		$this->assertTrue($route->isActive());
		$this->assertEquals($route->getKey(), $route->page->activeRoute->getKey());
	}

	/**
	 * @test
	 */
	public function publish_PageOnlyHasOneRoute()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();

		$page = $route->page->fresh();
		$page->publish(new PageTransformer);

		$this->assertCount(1, $page->routes);
	}

	/**
	 * @test
	 */
	public function publish_PublishedPageBakeContainsSerializedPageInstance()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$route->page->publish(new PageTransformer);

		$json = fractal($route->page, new PageTransformer)->parseIncludes([ 'blocks', 'canonical' ])->toJson();

		$this->assertEquals($json, $route->published_page->bake);
	}



	/**
	 * @test
	 */
	public function revert_WhenPageHasUnsavedChanges_ThrowsException()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$route->page->publish(new PageTransformer);

		$route->page->title = 'Foobar!';

		$this->expectException(Exception::class);
		$route->page->revert($route->page->published);
	}

	/**
	 * @test
	 */
	public function revert_WhenPublishedPageIsNotAssociatedWithPage_ThrowsException()
	{
		$r1 = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$r1->page->publish(new PageTransformer);

		$r2 = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$r2->page->publish(new PageTransformer);

		$this->expectException(Exception::class);
		$r1->page->revert($r2->page->published);
	}

	/**
	 * @test
	 */
	public function revert_RevertsPageToMatchPublishedPage()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$page = $route->page;

		$page->publish(new PageTransformer);

		$title = $page->title;
		$page->title = 'Foobar';

		$layout = $page->layout_name;
		$page->layout_name = 'fizzbuzz17';

		$page->save();
		$this->assertEquals('Foobar', $page->title);
		$this->assertEquals('fizzbuzz17', $page->layout_name);

		$page->revert($page->published);
		$this->assertEquals($title, $page->title);
		$this->assertEquals($layout, $page->layout_name);
	}

	/**
	 * @test
	 */
	public function revert_RevertsBlocksToMatchPublishedPage()
	{
		$route = factory(Route::class)->states([ 'withPage', 'isRoot' ])->create();
		$page = $route->page;

		$blocks = factory(Block::class, 2)->create([ 'page_id' => $page->getKey(), 'region_name' => 'test-region' ]);
		$page->publish(new PageTransformer);

		$moreBlocks = factory(Block::class, 3)->create([ 'page_id' => $page->getKey(), 'region_name' => 'test-region' ]);

{'fraction_non_alphanumeric': 0.2053812494181175, 'fraction_numerical': 0.003165440834186761, 'mean_word_length': 5.1312785388127855, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 279, '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': '5876026', 'n_tokens_mistral': 3846, 'n_tokens_neox': 3519, 'n_words': 583}