<?php

final class PhabricatorEditEngineSubtypeTransaction
  extends PhabricatorEditEngineTransactionType {

  const TRANSACTIONTYPE = 'editengine.config.subtype';

  public function generateOldValue($object) {
    return $object->getSubtype();
  }

  public function applyInternalEffects($object, $value) {
    $object->setSubtype($value);
  }

  public function getTitle() {
    $old = $this->getOldValue();
    $new = $this->getNewValue();

    return pht(
      '%s changed the subtype of this form from %s to %s.',
      $this->renderAuthor(),
      $this->renderOldValue(),
      $this->renderNewValue());
  }

  public function validateTransactions($object, array $xactions) {
    $map = $object->getEngine()
      ->setViewer($this->getActor())
      ->newSubtypeMap();

    $errors = array();
    foreach ($xactions as $xaction) {
      $new = $xaction->getNewValue();

      if ($map->isValidSubtype($new)) {
        continue;
      }

      $errors[] = $this->newInvalidError(
        pht('Subtype "%s" is not a valid subtype.', $new),
        $xaction);
    }

    return $errors;
  }

}

<?php

final class PhabricatorEditEngineSubtypeTransaction
  extends PhabricatorEditEngineTransactionType {

  const TRANSACTIONTYPE = 'editengine.config.subtype';

  public function generateOldValue($object) {
    return $object->getSubtype();
  }

  public function applyInternalEffects($object, $value) {
    $object->setSubtype($value);
  }

  public function getTitle() {
    $old = $this->getOldValue();
    $new = $this->getNewValue();

    return pht(
      '%s changed the subtype of this form from %s to %s.',
      $this->renderAuthor(),
      $this->renderOldValue(),
      $this->renderNewValue());
  }

  public function validateTransactions($object, array $xactions) {
    $errors = array();

    if (!$xactions) {
      return $errors;
    }

    $engine = $object->getEngine();

    if (!$engine->supportsSubtypes()) {
      foreach ($xactions as $xaction) {
        $errors[] = $this->newInvalidError(
          pht(
            'Edit engine (of class "%s") does not support subtypes, so '.
            'subtype transactions can not be applied to it.',
            get_class($engine)),
          $xaction);
      }
      return $errors;
    }

    $map = $engine
      ->setViewer($this->getActor())
      ->newSubtypeMap();

    foreach ($xactions as $xaction) {
      $new = $xaction->getNewValue();

      if ($map->isValidSubtype($new)) {
        continue;
      }

      $errors[] = $this->newInvalidError(
        pht('Subtype "%s" is not a valid subtype.', $new),
        $xaction);
    }

    return $errors;
  }

}

Fix incorrect construction of subtype map when validating "subtype" transactions against non-subtypable objects
Fix incorrect construction of subtype map when validating "subtype" transactions against non-subtypable objects

Summary:
Fixes T13389. Currently, we try to "newSubtypeMap()" unconditionally, even if the underlying object does not support subtypes.

  - Only try to build a subtype map if subtype transactions are actually being applied.
  - When subtype transactions are applied to a non-subtypable object, fail more explicitly.

Test Plan: Clicked "Make Editable" in a fresh Calendar transaction form, got an editable form instead of a fatal from "newSubtypeMap()". (Calendar events are not currently subtypable.)

Maniphest Tasks: T13389

Differential Revision: https://secure.phabricator.com/D20741
