/* bender-tags: editor,unit */
/* bender-ckeditor-plugins: autoid */

'use strict';

(function() {
  bender.editor = {
    config: {
      enterMode: CKEDITOR.ENTER_P,
      autoid: {
        autostart: false
      }
    }
  };

  bender.test({
    setUp: function() {
      this.editor = this.editorBot.editor;
    },

    tearDown: function() {
      this.editor.execCommand('autoid');
    },

    'test it can add a unique id to a heading': function() {
      var bot = this.editorBot,
        editor = this.editor,
        heading,
        resumeAfter = bender.tools.resumeAfter,
        startHtml = '<h1>This is a heading</h1>';

      resumeAfter(editor, 'allIdsComplete', function() {
        heading = editor.editable().findOne('h1');

        assert.isTrue(heading.hasAttribute('id'));
      });

      bot.setHtmlWithSelection(startHtml);
      editor.execCommand('autoid');

      wait();

    },

    'test it does not change the id of a heading that already has one': function() {
      var bot = this.editorBot,
        editor = this.editor,
        heading,
        resumeAfter = bender.tools.resumeAfter,
        startHtml = '<h1 id="12345">Heading with id</h1>';

      resumeAfter(editor, 'allIdsComplete', function() {
        heading = editor.editable().findOne('h1');

        assert.areSame(heading.getId(), '12345');
      });

      bot.setHtmlWithSelection(startHtml);
      editor.execCommand('autoid');

      wait();
    },

    'test it will add an id to a new heading': function() {
      var bot = this.editorBot,
        editor = this.editor,
        heading,
        resumeAfter = bender.tools.resumeAfter,
        startHtml = '<h1>This is a heading</h1>';

      bot.setHtmlWithSelection(startHtml);
      editor.execCommand('autoid');

      var newHeading = editor.document.createElement('h2');

      resumeAfter(editor, 'idAdded', function() {
        heading = editor.editable().findOne('h2');
        assert.isTrue(heading.hasAttribute('id'));
      });

      editor.insertElement(newHeading);
      editor.getSelection().selectElement(newHeading);

      wait();
    }
  });
})();

@@ -2080,24 +2080,820 @@
 wHeading);%0A%0A
+      wait();%0A    %7D,%0A%0A    'test it will add an id when a paragraph is converted to heading': function() %7B%0A      var bot = this.editorBot,%0A        editor = this.editor,%0A        paragraph,%0A        heading,%0A        style = new CKEDITOR.style(%7B element: 'h1' %7D),%0A        resumeAfter = bender.tools.resumeAfter,%0A        startHtml = '%3Cp%3EThis paragraph will be converted%5E%3C/p%3E';%0A%0A      bot.setHtmlWithSelection(startHtml);%0A      editor.execCommand('autoid');%0A%0A      paragraph = editor.editable().findOne('p');%0A      assert.isFalse(paragraph.hasAttribute('id'));%0A%0A      resumeAfter(editor, 'idAdded', function() %7B%0A        heading = editor.editable().findOne('h1');%0A%0A        assert.isTrue(heading.hasAttribute('id'));%0A      %7D);%0A%0A      // convert the paragraph to a heading%0A      editor.applyStyle(style);%0A%0A
       wait()

Add test to verify id will be generated on conversion to heading