/* bender-tags: editor,unit */
/* bender-ckeditor-plugins: link,toolbar */

// based off of dialog test for 'link' plugin
// https://github.com/ckeditor/ckeditor-dev/blob/master/tests/plugins/link/dialog.js

( function() {
	'use strict';

	function assertAnchorDiscovery( bot, expIds, expNames ) {
		bot.dialog( 'link', function( dialog ) {
			dialog.setValueOf( 'info', 'linkType', 'anchor' );

			var idOptions = dialog.getContentElement( 'info', 'anchorId' ).getInputElement().$.options,
				nameOptions = dialog.getContentElement( 'info', 'anchorName' ).getInputElement().$.options,

				names = [],
				ids = [];

			for ( var i = idOptions.length; i--; )
				ids.push( idOptions[ i ].value );

			for ( i = nameOptions.length; i--; )
				names.push( nameOptions[ i ].value );

			assert.areSame( expIds.sort().join( ',' ), ids.sort().join( ',' ), 'Anchor IDs discovered properly' );
			assert.areSame( expNames.sort().join( ',' ), names.sort().join( ',' ), 'Anchor names discovered properly' );
		} );
	}

	bender.editors = {
		framed: {
			name: 'framed',
			creator: 'replace',
			startupData: '<p>' +
				'<a id="aa" name="ab">a</a>' +
				'<a id="ba" name="bb">b</a>' +
				'<a id="ca" name="cb"></a>' +
			'</p>'
		},
		inline: {
			name: 'inline',
			creator: 'inline',
			startupData: '<p>' +
				'<a id="ma" name="mb">m</a>' +
				'<a id="na" name="nb">n</a>' +
				'<a id="oa" name="ob"></a>' +
			'</p>'
		},
		divarea: {
			name: 'divarea',
			creator: 'replace',
			startupData: '<p>' +
				'<a id="ta" name="tb">t</a>' +
				'<a id="ua" name="ub">u</a>' +
				'<a id="wa" name="wb"></a>' +
			'</p>',
			config: {
				extraPlugins: 'divarea'
			}
		}
	};

	bender.editorsConfig = {
		extraAllowedContent: 'a[id,name]'
	};

	bender.test( {
		tearDown: function() {
			CKEDITOR.dialog.getCurrent().hide();
		},

		'test discovery of anchors (framed)': function() {
			assertAnchorDiscovery( this.editorBots.framed,
				[ 'aa', 'ba', 'ca', '' ],
				[ 'ab', 'bb', 'cb', '' ] );
		},

		'test discovery of anchors (inline)': function() {
			assertAnchorDiscovery( this.editorBots.inline,
				[ 'ga', 'ha', 'ma', 'na', 'oa', 'ta', 'ua', 'wa', '' ],
				[ 'gb', 'hb', 'mb', 'nb', 'ob', 'tb', 'ub', 'wb', '' ] );
		},

		'test discovery of anchors (divarea)': function() {
			assertAnchorDiscovery( this.editorBots.divarea,
				[ 'ta', 'ua', 'wa', '' ],
				[ 'tb', 'ub', 'wb', '' ] );
		}
	} );
} )();

@@ -244,30 +244,31 @@
 ction assert
-Anchor
+Heading
 Discovery( b
@@ -282,20 +282,27 @@
 Ids, exp
-Name
+TextContent
 s ) %7B%0A%09%09
@@ -385,22 +385,23 @@
 Type', '
-anchor
+heading
 ' );%0A%0A%09%09
@@ -405,18 +405,23 @@
 %0A%09%09%09var 
-id
+heading
 Options 
@@ -461,16 +461,15 @@
 ', '
-anchorId
+heading
 ' ).
@@ -505,109 +505,19 @@
 %09%09%09%09
-nameOptions = dialog.getContentElement( 'info', 'anchorName' ).getInputElement().$.options,%0A%0A%09%09%09%09name
+textContent
 s = 
@@ -552,18 +552,23 @@
 var i = 
-id
+heading
 Options.
@@ -596,18 +596,23 @@
 s.push( 
-id
+heading
 Options%5B
@@ -606,32 +606,37 @@
 adingOptions%5B i 
+%5D%5B 1 
 %5D.value );%0A%0A%09%09%09f
@@ -644,20 +644,23 @@
 r ( i = 
-name
+heading
 Options.
@@ -682,20 +682,27 @@
 %09%09%09%09
-name
+textContent
 s.push( 
 name
@@ -697,20 +697,23 @@
 s.push( 
-name
+heading
 Options%5B
@@ -715,16 +715,21 @@
 ions%5B i 
+%5D%5B 0 
 %5D.value 
@@ -803,22 +803,23 @@
 ',' ), '
-Anchor
+Heading
  IDs dis
@@ -865,12 +865,19 @@
  exp
-Name
+TextContent
 s.so
@@ -894,20 +894,27 @@
  ',' ), 
-name
+textContent
 s.sort()
@@ -928,22 +928,23 @@
 ',' ), '
-Anchor
+Heading
  names d

Modify anchor discovery function for heading ids and text content