<?php

/**
 * A page cannot be created if not all required
 * fields are legal
 */

namespace ProcessWire\GraphQL\Test\Field\Mutation\CreatePage;

use ProcessWire\GraphQL\Utils;
use ProcessWire\GraphQL\Test\GraphQLTestCase;
use ProcessWire\NullPage;

class CaseOneTest extends GraphQLTestCase
{
  const settings = [
    "login" => "admin",
    "legalTemplates" => ["skyscraper", "city"],
    "legalFields" => ["featured", "height", "floors", "body"],
  ];

  public function testValue()
  {
    $query = 'mutation createPage ($page: SkyscraperCreateInput!) {
      createSkyscraper (page: $page) {
        name
        id
      }
    }';
    $variables = [
      "page" => [
        "parent" => "4121",
        "name" => "not-created-building-sky",
        "title" => "New Building Sky",
      ],
    ];
    $res = self::execute($query, $variables);
    $newBuildingSky = Utils::pages()->get("name=not-created-building-sky");
    self::assertEquals(
      2,
      count($res->errors),
      "createSkyscraper is not available if required `title` field is not legal."
    );
    assertStringContainsString(
      "SkyscraperCreateInput",
      $res->errors[0]->message
    );
    assertStringContainsString("createSkyscraper", $res->errors[1]->message);
    self::assertInstanceOf(
      NullPage::class,
      $newBuildingSky,
      "createSkyscraper does not create a page."
    );
  }
}
